91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
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}");
|
|
}
|
|
}
|
|
}
|