How do I add animations for my movement script?

Hello!

This is my first question here and I am kinda new to C# scripting, so I could use some clear answers.

My situation is that I have this movement script that I use for the player movement and a different script for the player facing direction.
I started to build with a simple one to see if everything works, but it doesn’t I can’t seem to find a solution.

using UnityEngine;

using System.Collections;

public class NewPlayerController : MonoBehaviour {

public float speed;

public float maxSpeed;

public AnimationClip idle;

public AnimationClip run;

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

	//Any one of thes if statements will make the character run. :)


		if (Input.GetKey("w")) 
			{
				print ("W is pressed");
			animation.CrossFade("run", 0.1F);
			if(rigidbody.velocity.magnitude < maxSpeed)
				{
					rigidbody.AddForce (Vector3.forward * speed);
				}
			}
		if (Input.GetKey("a"))
			{
				print ("A is pressed");
			animation.CrossFade("run", 0.1F);
				if(rigidbody.velocity.magnitude < maxSpeed)
				{
					rigidbody.AddForce (Vector3.left * speed);
				}
			}
		if (Input.GetKey("s"))
			{
				print ("S is pressed");
			animation.CrossFade("run", 0.1F);
				if(rigidbody.velocity.magnitude < maxSpeed)
				{
					rigidbody.AddForce (Vector3.back * speed);
				}
			}

		if (Input.GetKey ("d")) 
			{
				print ("D is pressed");
			animation.CrossFade("run", 0.1F);
				if (rigidbody.velocity.magnitude < maxSpeed) 
				{
					rigidbody.AddForce (Vector3.right * speed);
				}
			} 

	// Character is standing still... Kinda
		else 
			{
			animation.CrossFade("idle", 0.1F);
			}
	}
}

Now the problem is that while running it only plays the run animation when I walk right (When I press D), how do I work this out properly? It plays the idle animation when standing still.
I can also mention that it is a top down game, much like warlock brawl, a Warcraft 3 mod, or dota 2-like style but instead of moving by clicking I move by the WASD keys, the float-ish movement is meant to be.

Also, how can I add animations for walking backwards and sideways based on my characters facing position relative to what direction i move? (I am not sure if you need my facing script, but I will add it just in case…)

using UnityEngine;
using System.Collections;

public class PlayerDirection: MonoBehaviour
{
	
	// speed is the rate at which the object will rotate
	public float speed;
	
	void FixedUpdate () 
	{
		// Generate a plane that intersects the transform's position with an upwards normal.
		Plane playerPlane = new Plane(Vector3.up, transform.position);
		
		// Generate a ray from the cursor position
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		
		// Determine the point where the cursor ray intersects the plane.
		// This will be the point that the object must look towards to be looking at the mouse.
		// Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
		//   then find the point along that ray that meets that distance.  This will be the point
		//   to look at.
		float hitdist = 0.0f;
		// If the ray is parallel to the plane, Raycast will return false.
		if (playerPlane.Raycast (ray, out hitdist)) 
		{
			// Get the point along the ray that hits the calculated distance.
			Vector3 targetPoint = ray.GetPoint(hitdist);
			
			// Determine the target rotation.  This is the rotation if the transform looks at the target point.
			Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
			
			// Smoothly rotate towards the target point.
			transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
		}
	}
}

Thanks so much for stopping by and trying to help me! :slight_smile:

You have to use “else if” in the segments.
Try this:

using UnityEngine;
using System.Collections;
public class NewPlayerController : MonoBehaviour {
	public float speed;
	public float maxSpeed;
	public AnimationClip idle;
	public AnimationClip run;
	// Update is called once per frame
	void FixedUpdate() {
		//Any one of thes if statements will make the character run. :)
		if (Input.GetKey("w")) {
			print("W is pressed");
			animation.CrossFade("run", 0.1F);
			if (rigidbody.velocity.magnitude < maxSpeed) {
				rigidbody.AddForce(Vector3.forward * speed);
			}
		}else if (Input.GetKey("a")) {
			print("A is pressed");
			animation.CrossFade("run", 0.1F);
			if (rigidbody.velocity.magnitude < maxSpeed) {
				rigidbody.AddForce(Vector3.left * speed);
			}
		}else if (Input.GetKey("s")) {
			print("S is pressed");
			animation.CrossFade("run", 0.1F);
			if (rigidbody.velocity.magnitude < maxSpeed) {
				rigidbody.AddForce(Vector3.back * speed);
			}
		}else if (Input.GetKey("d")) {
			print("D is pressed");
			animation.CrossFade("run", 0.1F);
			if (rigidbody.velocity.magnitude < maxSpeed) {
				rigidbody.AddForce(Vector3.right * speed);
			}
		}else {
			animation.CrossFade("idle", 0.1F);
		}
	}
}

If you want to be able to combine several directions in the same time then try this script:

 using UnityEngine;
 using System.Collections;
 
 public class NewPlayerController : MonoBehaviour {
     public float speed;
     public float maxSpeed;
     public AnimationClip idle;
     public AnimationClip run;
 
     void FixedUpdate () {

         bool isMoving = false;

             if (Input.GetKey("w")) 
             {
                 print ("W is pressed");
                 animation.CrossFade("run", 0.1F);
                 if(rigidbody.velocity.magnitude < maxSpeed)
                 {
                     rigidbody.AddForce (Vector3.forward * speed);
                 }
                 isMoving = true;
             }
             if (Input.GetKey("a"))
             {
                 print ("A is pressed");
                 animation.CrossFade("run", 0.1F);
                 if(rigidbody.velocity.magnitude < maxSpeed)
                 {
                     rigidbody.AddForce (Vector3.left * speed);
                 }
                 isMoving = true;
             }
             if (Input.GetKey("s"))
             {
                 print ("S is pressed");
                 animation.CrossFade("run", 0.1F);
                 if(rigidbody.velocity.magnitude < maxSpeed)
                 {
                     rigidbody.AddForce (Vector3.back * speed);
                 }
                 isMoving = true;
             }
 
             if (Input.GetKey ("d")) 
             {
                 print ("D is pressed");
                 animation.CrossFade("run", 0.1F);
                 if (rigidbody.velocity.magnitude < maxSpeed) 
                 {
                     rigidbody.AddForce (Vector3.right * speed);
                 }
                 isMoving = true;
             }

             if(!isMoving)
             {
                 animation.CrossFade("idle", 0.1F);
             }
     }
 }

The boolean variable isMoving will take true if at least one direction button get pressed, else it will still false.

Note: Check this 1 to use Input.GetKey properly

Thank you so much for your response! It fixed everything, however do you have any idea how to detect if I move backwards or sideways?