0430 조이스틱으로 움직이고, 때리는 모션

2021. 4. 30. 16:39unity

using UnityEngine;
using UnityEngine.UI;

public class JoyStickTest : MonoBehaviour
{
    public VariableJoystick variableJoystick;

    public Transform heroTrans;
    public float moveSpeed=1f;
    public CharacterController player;
    private Vector3 dir;
    
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        StartCoroutine(RunImpl());
        StartCoroutine(AttackImpl());

        if (this.dir == Vector3.zero)
        {
            this.player.PlayAnimation("idle@loop");
        }

    }
    private IEnumerator RunImpl() 
    {
        this.dir = new Vector3(variableJoystick.Direction.x, 0, variableJoystick.Direction.y);
        if (this.dir != Vector3.zero)
        {
            var angle = Mathf.Atan2(dir.x, dir.z) * 180 / Mathf.PI;
            this.heroTrans.rotation = Quaternion.Euler(new Vector3(0, angle, 0));
            this.heroTrans.Translate(Vector3.forward * this.moveSpeed * Time.deltaTime);
            this.player.PlayAnimation("run@loop");
            yield return new WaitForSeconds(0.5f);
        }
    }

    private IEnumerator AttackImpl() 
    {
        if (Input.GetKeyDown(KeyCode.Space) && this.dir==Vector3.zero)
        {
            Debug.Log("attack");
            this.player.PlayAnimation("attack_sword_01");
            yield return null;

        }
    }
}
using UnityEngine;

public class CharacterController : MonoBehaviour
{
    public GameObject player;
    private Animation anim;
    private bool isRun=false;
    private Fov fov;
    private GameObject monster;

    // Start is called before the first frame update
    void Start()
    {
        this.anim = GetComponent<Animation>();
        if (isRun)
        {
            this.PlayAnimation("idle@loop");
        }
        this.monster = GameObject.FindGameObjectWithTag("Monster");
        this.fov = this.GetComponent<Fov>();
    }

    public void PlayAnimation(string animName) 
    {
        this.anim.Play(animName);
    }


    public void isInRange() 
    {        
        var toOther = monster.transform.position - this.transform.position;
        var angle = Vector3.Angle(this.transform.forward, toOther);
        var distance = toOther.magnitude;

        if (distance <= this.fov.area && angle < this.fov.angle / 2)
        {
            Debug.DrawLine(this.transform.position, monster.transform.position, Color.red);
        }
    }
    // Update is called once per frame
    void Update()
    {
        

    }
}

스페이스 바를 누르면 attack 문구가 뜨도록 구현, 그리고 움직이는 도중에 스페이스 바를 눌러도 attack 문구가 뜨지 않도록 구현

애니메이션만 재생하면 되는데 애니메이션이 왜 재생이 안 될까 ㅅㅂ...