C#/문제 해결

Unity) [유니티 2D] 애니메이션 연동

HSH12345 2023. 3. 4. 01:17

- 캐릭터의 4개의 각도에 애니메이션을 3개씩 구현하였다. 각각 int형식과 bool형식의 상태값을 활용하여 애니메이션의 변화를 주려고 하였지만 Run 애니메이션만 정상적으로 실행됨

 - Idle 애니메이션은 1프레임만 재생되고 다른 프레임은 재생이 안됨

 - 조건이 맞음에도 Stop 애니메이션이 실행 안됨

 - GunShell의 IsShoot 프로퍼티가 다른 스크립트에서는 false를 정상적으로 반환 하는데, GunShell 스크립트 내에서는 ture값만 반환하고 false값을 반환하지 못한다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunShell : MonoBehaviour
{
    private JoystickGunDir joystickGunDir;
    private float angle;
    public GameObject gun;
    public Animator anim;

    public float Angle { get { return angle; } }
    public bool IsShoot { get { return this.joystickGunDir.IsShoot; } }

    private void Start()
    {
        this.joystickGunDir = GameObject.FindObjectOfType<JoystickGunDir>();
    }

    private void Update()
    {
        if (this.joystickGunDir.Input == Vector3.zero) return;
        this.angle = Mathf.Atan2(this.joystickGunDir.Vertical, this.joystickGunDir.Horizontal) * Mathf.Rad2Deg;
        var lookRotation = Quaternion.Euler(angle * Vector3.forward);
        this.transform.rotation = lookRotation;

        //Debug.Log(this.angle);

        var gunSprite = this.gun.GetComponent<SpriteRenderer>();
        if (this.angle < -90f || this.angle > 90f) gunSprite.flipY = true;
        else gunSprite.flipY = false;

        if (this.angle < 130f && this.angle > 60f) gunSprite.sortingOrder = 9;
        else gunSprite.sortingOrder = 11;

        this.anim.SetBool("State", this.IsShoot);

        Debug.LogFormat("2 {0}", this.IsShoot);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerShell : MonoBehaviour
{
    private enum eLookState
    {
        Down, Left, Up, Right
    }

    public float playerMoveSpeed = 5f;
    public CharacterController cc;
    private JoystickMove joystickMove;
    public GunShell gunShell;
    public Animator anim;

    private eLookState lookState;

    void Start()
    {
        this.joystickMove = GameObject.FindObjectOfType<JoystickMove>();
        this.anim.SetInteger("LookState", 3);
        this.lookState = eLookState.Right;
    }

    private void Update()
    {
        Vector2 dir = new Vector2(this.joystickMove.Horizontal, this.joystickMove.Vertical);
        cc.Move(dir.normalized * this.playerMoveSpeed * Time.deltaTime);

        // -50 ~ -130, -130 ~ 130, 130 ~ 60, 60 ~ -50
        var angle = this.gunShell.Angle;

        if (angle < -50f && angle > -130f) this.lookState = eLookState.Down;
        else if (angle < -130f || angle > 130f) this.lookState = eLookState.Left;
        else if (angle < 130 && angle > 60) this.lookState = eLookState.Up;
        else if (angle < 60f && angle > -50) this.lookState = eLookState.Right;

        if ((int)this.lookState != this.anim.GetInteger("LookState"))
        {
            if (this.lookState == eLookState.Down) this.anim.SetInteger("LookState", 0);
            else if (this.lookState == eLookState.Left) this.anim.SetInteger("LookState", 1);
            else if (this.lookState == eLookState.Up) this.anim.SetInteger("LookState", 2);
            else if (this.lookState == eLookState.Right) this.anim.SetInteger("LookState", 3);  
        }

        if (dir != new Vector2(0, 0)) this.anim.SetBool("MoveState", true);
        else this.anim.SetBool("MoveState", false);

        this.anim.SetBool("ShootState", this.gunShell.IsShoot);
    }
}

 

 

 - 애니메이션 문제는 결국 모든 경우의 수를 전부 연결하여 해결

 - 프로퍼티 반환값 문제는 근본적으로는 해결하지 못하였으나, 다른 클래스에서 GunShell의 Animatior의 State 값을 설정하여 해결