using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Test_Play
{
public class PlayerController : MonoBehaviour
{
private enum eAnimState {
Idle, RunB, RunF, RunL, RunR
}
[SerializeField]
private float moveSpeed = 5f;
private Animation anim;
// Start is called before the first frame update
void Start()
{
this.anim = this.GetComponent<Animation>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxisRaw("Horizontal"); // x좌표 -1 ... 1
float v = Input.GetAxisRaw("Vertical"); // y좌표 -1 ... 1
Vector3 dir = new Vector3(h, 0, v);
Debug.Log(dir);
//이걸 이용해서 .. 키보드 입력으로 캐릭터 움직임
//dir에 정보들어있음
if(dir != Vector3.zero) // dir이 (0,0,0)이 아니라면
{
this.transform.Translate(dir * this.moveSpeed * Time.deltaTime);
this.PlayAnimation(dir);
}
}
private void PlayAnimation(Vector3 dir)
{
if(dir.x > 0)
{
//오른쪽
this.anim.CrossFade(eAnimState.RunR.ToString(), 0.25f);
}
else if(dir.x < 0)
{
//왼쪽
this.anim.CrossFade(eAnimState.RunL.ToString(), 0.25f);
}
else if(dir.z > 0)
{
//위
this.anim.CrossFade(eAnimState.RunF.ToString(), 0.25f);
}
else if(dir.z < 0)
{
//아래
this.anim.CrossFade(eAnimState.RunB.ToString(), 0.25f);
//애니메이션 이름, 시간
}
else
{
//정지
this.anim.CrossFade(eAnimState.Idle.ToString(), 0.25f);
}
}
}
}
가만히 있어도 Idle 모드를 안함..
'유니티 심화 > 복습' 카테고리의 다른 글
[복습 & 추가] HeroShooter - 몬스터 Indicator.. 잘모르는 것들 (0) | 2023.08.25 |
---|---|
[복습] HeroShooter - Main 만들기 + AngleAxis, Mathf.Atan2, Mathf.Rad2Deg (0) | 2023.08.24 |
[복습] Quaternion , OverlapSphere (0) | 2023.08.22 |
[복습] SpaceShooter - 마우스로 캐릭터 이동 (0) | 2023.08.18 |
[복습] SpaceShooter - 줌인, 줌아웃, 캐릭터 이동 (0) | 2023.08.18 |