캐릭터 애니메이션 추가(Idle, Run)

This commit is contained in:
2026-01-23 01:28:03 +09:00
parent 16497ffc6e
commit 2ceb28f55d
5 changed files with 2121 additions and 687 deletions

View File

@@ -7,74 +7,120 @@ 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.KeytAction -= OnKyeboard;
// Managers.Input.KeytAction += OnKyeboard;
Managers.Input.MouseAction -= OnMouseClicked;
Managers.Input.MouseAction += OnMouseClicked;
}
public enum PlayerState
{
Die,
Moving,
Idle,
}
PlayerState _state = PlayerState.Idle;
void UpdateDie()
{
// 아무것도 못함
}
void UpdateMoving()
{
Vector3 dir = _destPos - transform.position;
if (dir.magnitude < 0.0001f)
{
_state = PlayerState.Idle;
}
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);
}
// 애니메이션
Animator anim = GetComponent<Animator>();
// 현재 게임 상태에 대한 정보를 넘겨준다.
anim.SetFloat("speed", _speed);
}
void UpdateIdle()
{
// 애니메이션
Animator anim = GetComponent<Animator>();
anim.SetFloat("speed", 0);
}
void Update()
{
if (_moveToDest)
switch (_state)
{
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);
}
}
case PlayerState.Die:
UpdateDie();
break;
case PlayerState.Moving:
UpdateMoving();
break;
case PlayerState.Idle:
UpdateIdle();
break;
}
}
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 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)
if (_state == PlayerState.Die)
{
return;
}
// 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);
@@ -83,7 +129,7 @@ public class PlayerController : MonoBehaviour
if(Physics.Raycast(ray, out hit, 100.0f, LayerMask.GetMask("Wall")));
{
_destPos = hit.point;
_moveToDest = true;
_state = PlayerState.Moving;
// Debug.Log($"Raycast Camera @ {hit.collider.gameObject.tag}");
}
}