0
\$\begingroup\$

I've designed a Dodge behavior for my player. On input the behavior is executed setting "isDodging" bool to true. My Animation also plays when "isDodging" is true.

I need a proper way to set "isDodging" to false once the Behavior is complete. Currently "isDodging" becomes false once touching the ground is detected, but I think the best way to handle this would be once the animation is complete or the animation has been canceled into a different behavior. What steps would you recommend?

using UnityEngine;

public class Dodge : AbstractBehavior
{
    public float speed;
    public bool isDodging;

    private void FixedUpdate()
    {
        var canDodge = inputState.GetButtonValue(InputButtons[0]);

        if (!isDodging && canDodge)
            OnDodge();

        else if (isDodging && collisionState.standing)
            isDodging = false;
    }

    protected virtual void OnDodge()
    {
        var velocityX = speed * (float) inputState.direction;

        isDodging = true;
        body2D.velocity = new Vector2(velocityX, body2D.velocity.y);

    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

reference a Animator component and you can create a bool in the animation tab. to set it you can use anim.SetBool()

Animator anim

void Start{
anim = GetComponent<Animator>()
}

if (!isDodging && canDodge)
        OnDodge();

  //removed where you set to false

protected virtual void OnDodge()
{
    var velocityX = speed * (float) inputState.direction;

    isDodging = true;

    anim.SetBool("isDodging", isDodging);
    body2D.velocity = new Vector2(velocityX, body2D.velocity.y);

}
\$\endgroup\$
7
  • \$\begingroup\$ This seems to be what my animator is doing already, I set the animation state based on if my bool is true or false. is there a way to switch bools based on if the animation has started and finished? \$\endgroup\$
    – Pyreal
    Commented Jul 18, 2018 at 22:15
  • 1
    \$\begingroup\$ Double check your states in the animator tab. you can blend the animations or choose to just play the one. but the problem I bet is that you are touching ground and setting false. add another parameter in the anim tab to check against or isdodging should probably play from any state like a jump animation would. So it would play dodge then go right into the state it should be after its done playing \$\endgroup\$ Commented Jul 18, 2018 at 22:25
  • \$\begingroup\$ Thanks for the advice, I'll try it out. For things like my jump Animation, it checks to see if my Y-Velocity is higher than 0 and resets on touching the ground. My dodge and other behaviors aren't as easy to define what sets it to false so I need to solve those problems with a different method. \$\endgroup\$
    – Pyreal
    Commented Jul 18, 2018 at 22:28
  • \$\begingroup\$ It looks like you can set Animation events at the end of frames, any idea if this is a bad way to go? \$\endgroup\$
    – Pyreal
    Commented Jul 18, 2018 at 22:32
  • 1
    \$\begingroup\$ Good Job, Happy Coding! \$\endgroup\$ Commented Jul 18, 2018 at 22:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .