Setting an Animator movement float

I Changed the movement of my character to move with WASD, when setting the animator float i don’t know what to pass in, if I use “speed” the animator set him to run and he doesn’t stop, the “direction.magnitude” he stays idle. I really don’t want to use “set bool” lol, trying to keep the float because of the blend tree animations.

 void Update()
        {
           
            tpnavMeshAgent = GetComponent<NavMeshAgent>();
            tpanimator = GetComponent<Animator>();
            Move(direction);
        }

        public void Move(Vector3 direction)
        {
            if (tpcanMove == false) return;

                if (direction.magnitude >= 0.1f)
                {
                    float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
                    float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnspeedTime);
                    transform.rotation = Quaternion.Euler(0f, angle, 0f);

                    Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
                    tpnavMeshAgent.Move(moveDir.normalized * speed * Time.deltaTime);

                    UpdateAmimator();
                }

        }
        void UpdateAmimator()
        {
            tpanimator.SetFloat("Forward", direction.magnitude, .1f, Time.deltaTime);
        }

direction.magnitude is the variable to use… you may have to play around with the scale to fit your character… leave off the .1f and Time.deltaTime, instead, you’re looking for the approximate distance covered by the run animation in one cycle. This will vary from animation to animation so you’ll have to discover it through trial and error…
example:

float animationFactor = 2.0f; // it probably won't be this value, experiment until it looks right.

tpanimator.SetFloat("Forward", direction.magnitude*animationFactor);

Gave it a shot, still no luck, I made it a SerializeField so I can change it ingame.

What are the blend ranges in your animator?

So it looks like your magic multiplier should be 5.66… the real question is why isn’t that getting passed on to the Forward value

Was the animation graph working properly when it was click to move?

Yes, the enemies still use the Move from the click to move and they are on the same Animator as the player but they still work.

this is what the enemy code looks like.

        public void MoveTo(Vector3 destination, float speedFraction)
        {
            if (tpcanMove == false) return;

            navMeshAgent.destination = destination;
            navMeshAgent.speed = maxSpeed * Mathf.Clamp01(speedFraction);
            navMeshAgent.isStopped = false;
        }

        public void Cancel()
        {
            navMeshAgent.isStopped = true;
        }

        private void UpdateAmimator()
        {
            Vector3 velocity = navMeshAgent.velocity;
            Vector3 localVelocity = transform.InverseTransformDirection(velocity);
            float speed = localVelocity.z;
            GetComponent<Animator>().SetFloat("Forward", speed);
        }

I tried to use the same UpdateAnimator that the enemies use for the player but it doesn’t work, my guess is the the old Move is base on the navmeshagent’s destination and the player is not in a way. In short the enemies move fine from the same animation blend tree.

I just took a glance over your earlier code… and I’m wondering about the value of direction…
In Move(), direction is passed to the Move function, and calculations are made off of it… all the calls to direction in Move() are the local value inside Move(Vector3 direction)

Meanwhile in UpdateAnimator(), you’re basing your speed on direction.magnitude but where is this value coming from? It is clearly a global value… where is it set? Is it the same value as direction?

So i’ve been playing around with other ways to code movement to move the Player, in the Control script is the InputAxis for vertical and horizontal.

This is in the Update in the Control script and it calls to the Move

            float h = Input.GetAxisRaw("Horizontal");
            float v = Input.GetAxisRaw("Vertical");
            if (cam != null)
            {
                // calculate camera relative direction to move:
                camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
                move = v * camForward + h * cam.right;
            }
            else
            {
                // world-relative directions in the case of no main camera
                move = v * Vector3.forward + h * Vector3.right;
            }
            tpcharactermove.Move(move);

In the UpdateAmimator() I’ve tried passing in “tpanimator.SetFloat(“Forward”, speed);” the Player runs but doesn’t stop animating so it runs in place.

If I pass in “tpanimator.SetFloat(“Forward”, direction);” I get an error “cannot convert from UnityEngine.Vector to float”

this is on the Player

void Update()
        {
           
            tpnavMeshAgent = GetComponent<NavMeshAgent>();
            tpanimator = GetComponent<Animator>();
            Move(direction);
        }

        public void Move(Vector3 direction)
        {
            if (tpcanMove == false) return;

                if (direction.magnitude >= 0.1f)
                {
                    float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
                    float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnspeedTime);
                    transform.rotation = Quaternion.Euler(0f, angle, 0f);

                    Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
                    tpnavMeshAgent.Move(moveDir.normalized * speed * Time.deltaTime);

                    UpdateAmimator();
                }

        }
        void UpdateAmimator()
        {
            tpanimator.SetFloat("Forward", direction.magnitude * animationFactor);
        }

Nowhere in the above fragments of code is the direction that is used here assigned…
Try changing the header to UpdateAnimator to take in a Vector3…

void UpdateAnimator(Vector3 direction)
{
     tpanimator.SetFloat("Forward", direction.magnitude*animationFactor);
}

So the Player animation moves but it didnt stop and looking at my old code I see something i did not do to this new one, which is put the UpdateAnimator in void Update lol XD, so adding the Vector direction to the UpdateAnimator worked and i just needed to put it in void Update. Player clips to a stop tho.

Big thanks for your help.

1 Like

Sorry just noticed that when the Player speed is slowed, the animation doesn’t change because of the animationFactor, I’m brainstorming atm and was wondering if you had any ideas.

scale your animation factor to your current speed.

Thinking about what you said, I was going to make the animationFacter = the speed, then thought just put the speed in place of the animationFacter lol, so I took out the animationFacter. It works now so when the speed changes (slowed or sprinting) the animation scales with it.

thanks again.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms