Getting a value based on how far a control stick is pressed?

Hi, I’m using a blend tree for my idle/walk/run animation, and I’m using a joystick set up. I’ve got this for setting where I am in the blend tree;

mAnim.SetFloat(“Speed”, Math.Abs(Input.GetAxis(“Horizontal”)) + Math.Abs(Input.GetAxis(“Vertical”)));

Math.Abs helped since I was returning negative values half the time…

This works except for the fact that the diagonals are effectively twice as fast as the horizontals. What would be the easiest way to solve this?

Edit: You may want to look at this. It seems better (my way would only cap max speed) and more proper than the way I suggested.

Hi. New to all of this, so this may not be the best way to do this or even work. Just an idea. You could try creating a new variable called maxSpeed and then checking if speed is greater than maxSpeed and if so set it to maxSpeed.

Something like:

public int maxSpeed = 1;

void Update(){
if (Speed > maxSpeed)
{
Speed == maxSpeed;
}
}

Not so sure if that should be 2 ='s or 1, I think 2. Try the other if one doesn’t work. If neither work… sorry. I tried. Lol.

Eh, I could clamp the speed, but then I’d still get the issue of it hitting that max speed sooner on the actual joystick diagonals which would make it feel strange imo. I’ve seen the 0.7 number get thrown around in a few help threads, since this is a long-standing problem called crossing the square. I’ve also seen examples of a way to normalize the field, but I can’t seem to get the normalize function to work correctly due to variable constraints, ie it wants a float and I’m giving it Vector3 even with normalize…

Since I only have forward movement and the 3D model is rotated I guess I could just check whether the Math.Abs on the horizontal or the vertical axis is higher and then use whichever value is highest… I guess that’d get the result I’m looking for, I just know it’s a sloppy way to fix it.

EDIT: Using the sloppy method, tbh cant even tell if it fixed it, but I think so. If anyone knows a more efficient way let me know.

        if (Math.Abs(Input.GetAxis("Horizontal")) >= Math.Abs(Input.GetAxis("Vertical")))
        {
            mAnim.SetFloat("Speed", Math.Abs(Input.GetAxis("Horizontal")));
        }
        else
        {
            mAnim.SetFloat("Speed", Math.Abs(Input.GetAxis("Vertical")));
        }