C# Walking and running animation script

So i did a script the camera is okey but when i try to walk or run after 1-2 seonds later Animation stops but character moves.Can you guys help me?

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

public class MovementController : MonoBehaviour {
    private float speed;
    public float walkSpeed = 0.02f;
    public float runSpeed = 0.06f;
    public float rotationSpeed = 2.5f;

    Rigidbody rigidbody;
    Animator animator;
    CapsuleCollider capsuleCollider;

    public Transform cameraTransform;

    private float yaw = 0;
    private float pitch = 0;

    // Start is called before the first frame update
    void Start() {
        rigidbody = gameObject.GetComponent<Rigidbody>();
        animator = gameObject.GetComponent<Animator>();
        capsuleCollider = gameObject.GetComponent<CapsuleCollider>();

    }

    // Update is called once per frame
    void Update() {
        float z = Input.GetAxis("Vertical") * speed;
        float y = Input.GetAxis("Horizontal") * rotationSpeed;
        transform.Translate(0, 0, z);
        transform.Rotate(0, y, 0);

        yaw += rotationSpeed * Input.GetAxis("Mouse X");
        pitch -= rotationSpeed * Input.GetAxis("Mouse Y");
        transform.eulerAngles = new Vector3(0, yaw, 0);
        cameraTransform.eulerAngles = new Vector3(pitch, yaw, 0);

        if (Input.GetKey(KeyCode.LeftShift)) {
            if (Input.GetKey(KeyCode.W)) {
                animator.SetBool("IsStartWalking", false);
                animator.SetBool("IsIdle", false);
                animator.SetBool("IsWalking", false);
                animator.SetBool("IsRunning", true);
            }
            else {
                animator.SetBool("IsStartWalking", false);
                animator.SetBool("IsIdle", true);
                animator.SetBool("IsWalking", false);
                animator.SetBool("IsRunning", false);
            }
            speed = runSpeed;
        }
        else {
            if (Input.GetKey(KeyCode.W))
            {
                animator.SetBool("IsStartWalking", true);
                animator.SetBool("IsIdle", false);
                animator.SetBool("IsWalking", false);
                animator.SetBool("IsRunning", false);
                StartCoroutine(Wait());
                animator.SetBool("IsStartWalking", false);
                animator.SetBool("IsIdle", false);
                animator.SetBool("IsWalking", true);
                animator.SetBool("IsRunning", false);
            }
            else
            {
                animator.SetBool("IsStartWalking", false);
                animator.SetBool("IsIdle", true);
                animator.SetBool("IsWalking", false);
                animator.SetBool("IsRunning", false);
            }
            speed = walkSpeed;
        }
    }
    IEnumerator Wait() {
        yield return new WaitForSeconds(3);
    }
}

animator.SetBool(“IsStartWalking”, true);

animator.SetBool(“IsIdle”, false);

animator.SetBool(“IsWalking”, false);

animator.SetBool(“IsRunning”, false);

StartCoroutine(Wait());

animator.SetBool(“IsStartWalking”, false);

animator.SetBool(“IsIdle”, false);

animator.SetBool(“IsWalking”, true);

animator.SetBool(“IsRunning”, false);

here i wanted to try that first i want to use the StartWalking Animation than i want to use Walking Animation.Is this true or not?

edit
i solved the running Animation Problem but the others are unsolved

Your coroutine doesn’t do anything. If you want the code after you call StartCoroutine to be delayed for 3 seconds, you need to move it inside the coroutine after the wait:

    IEnumerator Wait() {
        yield return new WaitForSeconds(3);
        animator.SetBool("IsStartWalking", false);
        animator.SetBool("IsIdle", false);
        animator.SetBool("IsWalking", true);
        animator.SetBool("IsRunning", false);
    }
1 Like

Thanks man and is there a way to use float for my wait time?

Of course - WaitForSeconds accepts a floating point number. So you could pass that in as a parameter to your coroutine:

IEnumerator Wait(float waitTime) {
    yield return new WaitForSeconds(waitTime);
    animator.SetBool("IsStartWalking", false);
    animator.SetBool("IsIdle", false);
    animator.SetBool("IsWalking", true);
    animator.SetBool("IsRunning", false);
}

Then you can pass in whatever you want when you start the coroutine:

StartCoroutine(Wait(3.45f));
1 Like

Thanks man this is harder than it Looks :smile:

sing System.Collections.Generic;
using UnityEngine;

public class MovementController : MonoBehaviour {
    private float speed;
    public float walkSpeed = 0.02f;
    public float runSpeed = 0.06f;
    public float rotationSpeed = 2.5f;

    Rigidbody rigidbody;
    Animator animator;
    CapsuleCollider capsuleCollider;

    public Transform cameraTransform;

    private float yaw = 0;
    private float pitch = 0;

    // Start is called before the first frame update
    void Start() {
        rigidbody = gameObject.GetComponent<Rigidbody>();
        animator = gameObject.GetComponent<Animator>();
        capsuleCollider = gameObject.GetComponent<CapsuleCollider>();

    }

    // Update is called once per frame
    void Update() {
        float z = Input.GetAxis("Vertical") * speed;
        float y = Input.GetAxis("Horizontal") * rotationSpeed;
        transform.Translate(0, 0, z);
        transform.Rotate(0, y, 0);

        yaw += rotationSpeed * Input.GetAxis("Mouse X");
        pitch -= rotationSpeed * Input.GetAxis("Mouse Y");
        transform.eulerAngles = new Vector3(0, yaw, 0);
        cameraTransform.eulerAngles = new Vector3(pitch, yaw, 0);

        if (Input.GetKey(KeyCode.LeftShift)) {
            if (Input.GetKey(KeyCode.W)) {
                animator.SetBool("IsStartWalking", false);
                animator.SetBool("IsIdle", false);
                animator.SetBool("IsWalking", false);
                animator.SetBool("IsRunning", true);
            }
            else {
                animator.SetBool("IsStartWalking", false);
                animator.SetBool("IsIdle", true);
                animator.SetBool("IsWalking", false);
                animator.SetBool("IsRunning", false);
            }
            speed = runSpeed;
        }
        else {
            if (Input.GetKey(KeyCode.W))
            {
                animator.SetBool("IsStartWalking", true);
                animator.SetBool("IsIdle", false);
                animator.SetBool("IsWalking", false);
                animator.SetBool("IsRunning", false);
                StartCoroutine(Wait(2.57f));
               
            }
            else
            {
                animator.SetBool("IsStartWalking", false);
                animator.SetBool("IsIdle", true);
                animator.SetBool("IsWalking", false);
                animator.SetBool("IsRunning", false);
            }
            speed = walkSpeed;
        }
        if (Input.GetKey(KeyCode.A))
        {
            animator.SetBool("IsIdle", false);
            animator.SetBool("IsWalkingRight", false);
            animator.SetBool("IsWalkingLeft", true);
            speed = walkSpeed;
        }
        else
        {
            animator.SetBool("IsIdle", true);
            animator.SetBool("IsWalkingRight", false);
            animator.SetBool("IsWalkingLeft", false);
        }

        if (Input.GetKey(KeyCode.D)) {
            animator.SetBool("IsIdle", false);
            animator.SetBool("IsWalkingRight", true);
            animator.SetBool("IsWalkingLeft", false);
            speed = walkSpeed;
        }
        else {
            animator.SetBool("IsIdle", true);
            animator.SetBool("IsWalkingRight", false);
            animator.SetBool("IsWalkingLeft", false);
        }
           
       
    }
    IEnumerator Wait(float waitTime) {
        yield return new WaitForSeconds(waitTime);
        animator.SetBool("IsStartWalking", false);
        animator.SetBool("IsIdle", false);
        animator.SetBool("IsWalking", true);
        animator.SetBool("IsRunning", false);
    }
}

Now my “D” button “WalkingLeft” doesn’t work any idea?