0
votes

In the process of studying Game Development, I decided to do a game of my own. However, while I was putting my animations on my model through the "Animator" tab, I stumbled upon a problem. I have created a parameter of type "float" in the animator, where if the speed is greater or less than the value x, it plays a certain animation. However, in order to instantiate the walking/running speed, I am using a field which is located in the Inspector tab. The problem is that since the initialized speed is always different than 0, the animator uses that inserted value and plays the walking animation despite the fact that no key is pressed!

I have already tried various things that I found online such as, using the "Parameter" checkbox on the animations or using different lines of code such as "animator.SetFloat("Speed", (speed));" on my script but none of these have worked out.

// Update is called once per frame
void Update()
{
    //animator.SetFloat("Speed", Mathf.Abs(speed));
    animator.SetFloat("Speed", (speed));

    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");

    Vector3 moveDirection = new Vector3(horizontal, 0f, vertical) * speed * Time.deltaTime;
    transform.Translate(moveDirection);

I expect the output to be as follows: when no key is pressed, Idle animation to play. when WASD keys are pressed, walking animation to play. when Shift + WASD keys are pressed, running animation to play.

1

1 Answers

0
votes

In the case you describe you might want to consider not using transitions at all but rather do it all by code using animator.Play or maybe even animator.CrossFade for transitions

public class ExampleEditor : MonoBehaviour
{
    private Animator animator;

    private void Update()
    {
        if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
        {
            animator.Play("Running");
            return;
        }

        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
        {
            animator.Play("Walking");
            return;
        }

        animator.Play("Idle");
    }
}

or if you rather want to use the parameters you could instead use bools like

public class ExampleEditor : MonoBehaviour
{
    private Animator animator;

    private void Update()
    {
        if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
        {
            animator.SetBool("Running", true);
            return;
        }

        animator.SetBool("Running", false);

        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
        {
            animator.SetBool("Walking", true);
            return;
        }

        animator.SetBool("Walking", false);
    }
}

and have transition conditions like

IDLE -> Running if Running=true
IDLE -> Walking if Running=false && Walking=true

Running -> Walking if Walking=true && Running=false
Running -> IDLE if Walking=false && Running=false

Walking -> Running if Running=true
Walking -> IDLE if Walking=false