Unity/유니티 기본

[4.29] 미니프로젝트 1 - 조이스틱으로 플레이어 움직이기 및 공격모션

ljw4104 2021. 4. 29. 13:21

  1. 캐릭터는 공격중에 움직일 수 없다.
  2. 공격 모션이 끝난뒤에 움직일 수 있다.
  3. Animation 컴포넌트 너무 끔찍하다. Animator로 바꿔봤는데 동작하지 않음 왜 그런지 알아봐야됨.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JoystickTest : MonoBehaviour
{
    public enum eState
    {
        Idle, Run, Attack
    }

    public Transform hero;
    public Animation anim;
    public eState state;

    public float speed = 2f;
    public VariableJoystick joystick;
    // Start is called before the first frame update
    void Start()
    {
        this.state = eState.Idle;
    }

    // Update is called once per frame
    void Update()
    {
        if (this.state != eState.Attack && Input.GetKeyDown(KeyCode.Space))
        {
            this.state = eState.Attack;
            StartCoroutine(this.Attack());
        }

        if(this.state != eState.Attack)
        {
            var dir = new Vector3(this.joystick.Direction.x, 0, this.joystick.Direction.y);

            if (dir != Vector3.zero)
            {
                this.state = eState.Run;

                this.anim.Play("run@loop");
                Vector3 movement = new Vector3(this.joystick.Direction.x, 0.0f, this.joystick.Direction.y);
                this.hero.rotation = Quaternion.LookRotation(movement);
                this.hero.Translate(movement * this.speed * Time.deltaTime, Space.World);
            }
            else
            {
                this.anim.Play("idle@loop");
            }
        }
    }

    public IEnumerator Attack()
    {
        this.anim.Play("attack_sword_01");
        yield return new WaitForSeconds(0.733f);
        this.state = eState.Idle;
    }
}

 

*참고 사이트

answers.unity.com/questions/803365/make-the-player-face-his-movement-direction.html

 

Make the player face his movement direction - Unity Answers

 

answers.unity.com