Backwards movement

So ive tried several different methods for enabling a backwards moving animation when i am moving backwards the animation will activate when the attackanimation speed float reaches 12 however i cant seem to get this to work and im sure there is an easier way

this is the animation portion of my script

 void Animate()
    {
        if (Input.GetKey(KeyCode.LeftShift))
        {
            attackanimaton.SetFloat("Speed", 9.0f);
            navAgent.speed = 7;
        }

        if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Vertical") > 0)
        {
            attackanimaton.SetBool("isAttacking", false);
            attackanimaton.SetFloat("Speed", 6.0f);
            navAgent.speed = 4;

        }

        else if (navAgent.velocity.magnitude < 1.0f && (!Input.GetKey(KeyCode.LeftShift)))

        {
            navAgent.speed = 1;
            attackanimaton.SetFloat("Speed", 1.0f);
        }
    }

movement script

    void movement()
    {
        var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
        var z = Input.GetAxis("Vertical") * Time.deltaTime * walkspeedstat;

        transform.Rotate(0, x, 0);
        transform.Translate(0, 0, z);


    }

Hi there,

You mentioned:

looking at the code above, I can’t see when the animator property speed will ever be 12.

As far as I can see you only ever set animator property speed as either 9.0f, 6.0f or 1.0f.

What I think you want is something along the lines of:

either explicity setting “Speed” to 12.0f somewhere to trigger the animation.

or

[ EDIT ] Apologies I just noticed you are using a NavMeshAgent, the following may not apply. Hopefully the statement above is helpful. [/EDIT]

            var speed = InputGetAxis("Vertical") * Time.deltaTime * walkspeedstat;
            attackanimaton.SetBool("isAttacking", false);
            attackanimaton.SetFloat("Speed", speed);
            navAgent.speed = speed;

speed will be positive when moving forwards and negative when moving backwards and the Animator property Speed will increase and decrease with this speed.

I’m not sure how you have your Animator setup. If you have transitions set with “equals” then you may need to change it to greater than and lesser than.

What I do in my Animator is use a Blend Tree to smoothly blend between backwards - idle and forward animations. Worth a google if you are interested.

Kind Regards,
Joe

thanks i just made a separate method to get it to work but thanks the theory behind this method will be a huge benefit to me in the future

1 Like