Unity) [유니티 2D]


– 캐릭터의 4가지 시점에 각각 3가지 애니메이션이 구현되었습니다. int와 bool 타입의 상태 값을 각각 사용하여 애니메이션을 변경해 보았지만 실행 애니메이션만 정상적으로 수행되었습니다.

– 대기 애니메이션은 1프레임만 재생되고 나머지 프레임은 재생되지 않습니다.

– 조건을 만족해도 정지 애니메이션이 실행되지 않음


– GunShell의 IsShoot 속성은 일반적으로 다른 스크립트에서는 false를 반환하지만 GunShell 스크립트에서는 true 값만 반환하고 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);
    }
}