Animations Do Not Stop Playing

Unity version is 3.5
The problem I am having is that when I go forward backwards left or right and then change directions my animations don’t stop and even when I don’t change the character will stop and is running staring or moving backwards in place.
Not really sure what I did wrong.
Here is my script.

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour
{

	public float mySpeed;
	public float rotateSpeed;

	public static CharacterController characterController;



	void Start()
	{
		characterController = GetComponent<CharacterController>();
		animation.CrossFade("Idle");
	}

	void Update()
	{
		if (Input.GetAxis("Vertical") > 0)
		{
			characterController.Move(transform.forward * mySpeed * Time.deltaTime);
			animation.CrossFade("Running");
			Debug.Log("Forward");
		}
		else if (Input.GetAxis("Vertical") < 0)
		{
			characterController.Move(transform.forward * -mySpeed * Time.deltaTime);
			animation.CrossFade("WalkBackwards");
			Debug.Log("Backwards");
		}
		if (Input.GetAxis("Horizontal") > 0)
		{
			characterController.Move(transform.right * mySpeed * Time.deltaTime);
			animation.CrossFade("StrafingRight");
			Debug.Log("Right");
		}
		else if (Input.GetAxis("Horizontal") < 0)
		{
			characterController.Move(transform.right * -mySpeed * Time.deltaTime);
			animation.CrossFade("StrafingLeft");
			Debug.Log("Left");
		}
			transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0));
	}

}

1 Answer

1

I figured it out and it’s so obvious now that I think about it.

All I had to do was this.

 void Start()
{
characterController = GetComponent<CharacterController>();
animation.CrossFade("Idle");  //cut this out
}

void Update()
{
  animation.CrossFade("Idle"); //and put it here
 }

The obvious part (to me anyway)is that my script was never checking to see if the player stopped moving and to play the idle animation.

I am sure there is better ways to do this but it worked