개인적인 공부/Unity

Shooting Animation

karatejin 2023. 5. 22. 18:09

Sprite 의 그림을 animation 한 후 animator 의 parameter 에 넣고 처음에는 SetBool 로 Shooting 을 true, false로 전환하려고 했는데 원하는대로 되지 않았다.

 

true
Set Bool  false

void OnFire(InputValue value)
    {
        if (!isAlive)
        {
            return;
        }
        Instantiate(bullet, gun.position, transform.rotation);

        if(value.isPressed)
        {
            myAnimator.SetBool("isShooting",true);
        }
        else
        {
            myAnimator.SetBool("isShooting",false);
        }
    }
    
    ////////
    if(value.isPressed)
        {
            myAnimator.SetBool("isShooting",true);
        }
    if(!value.isPressed)
    	{
        	myAnimator.SetBool("isShooting",false);
        }

 

문제의 한번 누른후 idling 상태로 돌아가지 않는다.

Bool 대신 Trigger 로 변환해서 작동시켰다.

 

SetTrigger
ResetTrigger
ResetTrigger Inspector

수정한 코드

void OnFire(InputValue value)
    {
        if (!isAlive)
        {
            return;
        }
        Instantiate(bullet, gun.position, transform.rotation);

        if (value.isPressed)
        {
            myAnimator.SetTrigger("isShooting");
        }
        else
        {
            myAnimator.ResetTrigger("isShooting");
        }
    }

결과