0
votes

Hello so i have this script and in the void update in doesnt matter wich is first for backwards or run up it always makes the animator play the animation backwards walk or run it plays the animation half way or full way and it plays part of the idle state without it been called that means your finger is still in the on the button so it must still play backward/forwards animation over and over . here is the code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerController : MonoBehaviour {
    public float moveSpeed = 10f;
    public float turnSpeed = 50f;
    Animator anim;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator> ();

    }

    // Update is called once per frame
    void Update () {


        if (Input.GetKey (KeyCode.S)) {
            anim.SetBool ("isIdle", false);
            anim.SetBool ("isWalkingBack", true);
            transform.Translate (-Vector3.forward * moveSpeed * Time.deltaTime);


        }
        else
        {
            anim.SetBool ("isIdle", true);
            anim.SetBool ("isWalkingBack", false);

        }

        if (Input.GetKey (KeyCode.W)) {
            anim.SetBool ("isRunning", true);
            anim.SetBool ("isIdle", false);
            transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);




        }
        else
        {

            anim.SetBool ("isRunning", false);
            anim.SetBool ("isIdle", true);

        }



}


}
`
2

2 Answers

0
votes

Using boolean fields may cause some problems, for example, which you described above.

I recommend to use: anim.SetInteger("unitState", someIntValue);

Configure the connections and transitions in the animator to work with the field "unitState".

In your code it will look something like this:

void Update () {
    // for example
    anim.SetInteger("unitState", 0); // 0 is Idle
    if (Input.GetKey (KeyCode.S)) {
        anim.SetInteger("unitState", -1); // -1 is WalkBack
        transform.Translate (-Vector3.forward * moveSpeed * Time.deltaTime);
    }

    if (Input.GetKey (KeyCode.W)) {
        anim.SetInteger("unitState", 1); // 1 is run forward
        transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
    }

    if (Input.GetKeyDown (KeyCode.Space)) {
        anim.SetInteger("unitState", 2); // 2 is jump
        //Jump code here. for example
    }
    ....
}
0
votes

Make sure you have unchecked exit time on every transition.