유니티 심화/복습

[복습] SpaceShooter - Idle 오류

다모아 2023. 8. 18. 00:26
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 모드를 안함..