0
\$\begingroup\$

This is my first time in Unity3D and programming so forgive me if i'm a bit vague on this. I'm working on my own project in Unity3D but I couldn't get the player to stop moving when I press the key. I avoided using the GetButton since it triggered the animation to play twice. After bout 2 hours, I still can't find any answer to my problem.

Can someone help me please?

Here's part of the code of the the player script.

    if (Input.GetButtonDown("Fire1"))
    {
        animator.SetTrigger("AttackSword");
        Debug.Log("Animation called");
    }

    if(!animator.GetBool("AttackSword"))
    {
        Move(inputDir, running);
    }

If you would like me to add the full code of the script, let me know.

\$\endgroup\$
  • 1
    \$\begingroup\$ I see you're using both SetTrigger and GetBool with the same variable name. Which kind of variable is it, a boolean, or a trigger? \$\endgroup\$ – DMGregory Jun 6 '18 at 10:10
  • \$\begingroup\$ @DMGregory i'm using a trigger to activate it. \$\endgroup\$ – DigitalWolf Jun 6 '18 at 12:11
  • \$\begingroup\$ Ah, that will do it. Triggers don't have a persistent 'On" state that can last over a period of time, like a boolean does. They're an instantaneous signal to start a transition. \$\endgroup\$ – DMGregory Jun 6 '18 at 12:14
  • \$\begingroup\$ @DMGregory I understand that but the problem i'm facing is that, how do I set the trigger to true aka "On" state temporarily so that the player stops moving while the attack animation plays? The GetButton causes the animation to run twice for some reason like I said above. The GetButtonDown enables the animation to run, but doesn't enable the trigger state to true. I was wondering if there was any way or alternative that I can use to overcome this problem. \$\endgroup\$ – DigitalWolf Jun 6 '18 at 12:29
1
\$\begingroup\$

I decided to experiment other codes and see if it works and I found out about this code and it works like a charm.

    if (!this.animator.GetCurrentAnimatorStateInfo(0).IsName("AttackSword"))
    {
        Move(inputDir, running);
    }

Even though the transition isn't smooth, I still find it satisfying to solve this problem on my own.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.