충돌, 레이캐스팅, 레이어, 카메라 기능 구현
This commit is contained in:
40
Assets/Scripts/Controllers/CameraController.cs
Normal file
40
Assets/Scripts/Controllers/CameraController.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
Define.Cameramode _mode = Define.Cameramode.QuaterView;
|
||||
[SerializeField]
|
||||
Vector3 _delta = new Vector3(0.0f, 6.0f, -5.0f);
|
||||
[SerializeField]
|
||||
GameObject _player = null;
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
if (_mode == Define.Cameramode.QuaterView)
|
||||
{
|
||||
RaycastHit hit;
|
||||
if(Physics.Raycast(_player.transform.position, _delta, out hit, _delta.magnitude, LayerMask.GetMask("Wall")))
|
||||
{
|
||||
float dist = (hit.point - _player.transform.position).magnitude * 0.8f;
|
||||
transform.position = _player.transform.position + _delta.normalized * dist;
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.position = _player.transform.position + _delta;
|
||||
transform.LookAt(_player.transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetQuaterView(Vector3 delta)
|
||||
{
|
||||
_mode = Define.Cameramode.QuaterView;
|
||||
_delta = delta;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Controllers/CameraController.cs.meta
Normal file
2
Assets/Scripts/Controllers/CameraController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f04a4f1be0f3364478dd7276aebb2f24
|
||||
90
Assets/Scripts/Controllers/PlayerController.cs
Normal file
90
Assets/Scripts/Controllers/PlayerController.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Numerics;
|
||||
using TreeEditor;
|
||||
using UnityEngine;
|
||||
using Quaternion = UnityEngine.Quaternion;
|
||||
using Vector3 = UnityEngine.Vector3;
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
float _speed = 10.0f;
|
||||
|
||||
bool _moveToDest = false;
|
||||
private Vector3 _destPos;
|
||||
|
||||
void Start()
|
||||
{
|
||||
Managers.Input.KeytAction -= OnKyeboard;
|
||||
Managers.Input.KeytAction += OnKyeboard;
|
||||
Managers.Input.MouseAction -= OnMouseClicked;
|
||||
Managers.Input.MouseAction += OnMouseClicked;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_moveToDest)
|
||||
{
|
||||
Vector3 dir = _destPos - transform.position;
|
||||
if (dir.magnitude < 0.0001f)
|
||||
{
|
||||
_moveToDest = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
float moveDist = Mathf.Clamp(_speed * Time.deltaTime, 0, dir.magnitude);
|
||||
transform.position += dir.normalized * moveDist;
|
||||
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), 20 * Time.deltaTime);
|
||||
// transform.LookAt(_destPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnKyeboard()
|
||||
{
|
||||
if (Input.GetKey(KeyCode.W))
|
||||
{
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
|
||||
transform.position += (Vector3.forward * Time.deltaTime * _speed);
|
||||
}
|
||||
|
||||
if (Input.GetKey(KeyCode.S))
|
||||
{
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
|
||||
transform.position += (Vector3.back * Time.deltaTime * _speed);
|
||||
}
|
||||
|
||||
if (Input.GetKey(KeyCode.A))
|
||||
{
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
|
||||
transform.position += (Vector3.left * Time.deltaTime * _speed);
|
||||
}
|
||||
|
||||
if (Input.GetKey(KeyCode.D))
|
||||
{
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
|
||||
transform.position += (Vector3.right * Time.deltaTime * _speed);
|
||||
}
|
||||
|
||||
_moveToDest = false;
|
||||
}
|
||||
|
||||
void OnMouseClicked(Define.MouseEvent evt)
|
||||
{
|
||||
if (evt != Define.MouseEvent.Click)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
|
||||
Debug.DrawRay(Camera.main.transform.position, ray.direction * 100.0f, Color.red, 1.0f);
|
||||
|
||||
RaycastHit hit;
|
||||
if(Physics.Raycast(ray, out hit, 100.0f, LayerMask.GetMask("Wall")));
|
||||
{
|
||||
_destPos = hit.point;
|
||||
_moveToDest = true;
|
||||
// Debug.Log($"Raycast Camera @ {hit.collider.gameObject.tag}");
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Controllers/PlayerController.cs.meta
Normal file
2
Assets/Scripts/Controllers/PlayerController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 07c76605d2feab34488f71316e532b7f
|
||||
Reference in New Issue
Block a user