applying animations on a top-down 2d game

i’ve recently started to learn coding alongside this project to make a top-down rpg,and i am having some trouble assigning my walking and idling animations to my character.
first off i have used the following code to make the movement so i can make the movement grid based and disable diagonals and disable moving to a collider

using System.Collections;
using UnityEngine;

class Movementt : MonoBehaviour {
	public float moveSpeed = 3f;
	public float runSpeed = 6f;

	private float gridSize = 1f;
	private bool allowDiagonals = false;
	private bool correctDiagonalSpeed = true;
	private Vector2 input;
	private bool isMoving = false;
	private Vector2 startPosition;
	private Vector2 endPosition;
	private float t;
	private float factor;


	public void Start ()
	{

		}




	public void FixedUpdate() {
		if (!isMoving) {
			input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
			if (!allowDiagonals) {
				if (Mathf.Abs(input.x) > Mathf.Abs(input.y)) {
					input.y = 0;
				} else {
					input.x = 0;
				}
			}

			
			if (input != Vector2.zero) {
				StartCoroutine(move(transform));
			}
		}
	}
	
	public IEnumerator move(Transform transform) {
		isMoving = true;

		t = 0;
		

			
		
		if(allowDiagonals && correctDiagonalSpeed && input.x != 0 && input.y != 0) {
			factor = 0.7071f;
		} else {
			factor = 1f;
		}
		startPosition = rigidbody2D.transform.position;
		endPosition = new Vector2 (startPosition.x + System.Math.Sign (input.x) * gridSize,
		                           startPosition.y + System.Math.Sign (input.y) * gridSize);
		RaycastHit2D right = Physics2D.Raycast (startPosition, Vector2.right, 1f);
		RaycastHit2D left = Physics2D.Raycast (startPosition, -Vector2.right, 1f);
		RaycastHit2D up = Physics2D.Raycast (startPosition, Vector2.up, 1f);
		RaycastHit2D down = Physics2D.Raycast (startPosition, -Vector2.up, 1f);


		if (right.collider != null && endPosition.x > startPosition.x) {
						endPosition = startPosition;
		}
		if (left.collider != null && endPosition.x < startPosition.x) {
			endPosition = startPosition;
		}
		if (up.collider != null && endPosition.y > startPosition.y) {
			endPosition = startPosition;
		}
		if (down.collider != null && endPosition.y < startPosition.y) {
			endPosition = startPosition;
		}





		while (t < 1f)
		{
				if (Input.GetKey (KeyCode.LeftShift)) {
			t += Time.deltaTime * (runSpeed / gridSize) * factor;
						}
		else {
				t += Time.deltaTime * (moveSpeed/gridSize) * factor;
			}
			transform.position = Vector2.Lerp(startPosition, endPosition, t);
			yield return null;
		}
		
		isMoving = false;
		yield return 0;
	}

}

but since my characters is 2 units long and i want it to raycast from the feet i added the character as a child entity with y=1,and added the following code to try and animate it

using UnityEngine;
using System.Collections;

public class playeranimation : MonoBehaviour {
	Animator anim;
	private Vector2 input;
	// Use this for initialization
	private float compareX;
	private float compareY;
	void Start () {
		anim = GetComponent<Animator> ();
	}
	
	// Update is called once per frame
	void fixedUpdate () {
		input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
		if (Mathf.Abs (input.x) > Mathf.Abs (input.y)) {
						input.y = 0;
				} else {
						input.x = 0;
				}
		compareX = input.x;
		compareY = input.y;
		if (compareX > 0) {
						if (Input.GetKey (KeyCode.LeftShift)) {
								anim.SetFloat ("MoveDirection", 2);
						} else {
								anim.SetFloat ("MoveDirection", 1);
						}

						anim.SetFloat ("IdleDirection", 2f);
				}
		if (compareX < 0) {
			if (Input.GetKey (KeyCode.LeftShift)) {
				anim.SetFloat ("MoveDirection", -2);
			} else {
				anim.SetFloat ("MoveDirection", -1);
			}
			
			anim.SetFloat ("IdleDirection", 3f);
		}
		if (compareY > 0) {
			if (Input.GetKey (KeyCode.LeftShift)) {
				anim.SetFloat ("MoveDirection", 4);
			} else {
				anim.SetFloat ("MoveDirection", 3);
			}
			
			anim.SetFloat ("IdleDirection", 0f);
		}
		if (compareY < 0) {
			if (Input.GetKey (KeyCode.LeftShift)) {
				anim.SetFloat ("MoveDirection", -4);
			} else {
				anim.SetFloat ("MoveDirection", -3);
			}
			
			anim.SetFloat ("IdleDirection", 1f);
		}
		if (input == Vector2.zero) {
						anim.SetFloat ("Movedirection", 0);
				}






	}
}

and although i set these values to the animations none play whatsoever

updated the question with a bit more details

2 Answers

2

Try this, more easy code and later do a tutorial about blend tree. It works for me. You will move in 8 directions.

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

	private Animator anim;
public Vector2 speed = new Vector2(4, 4);

private Vector2 movement;

		void Start ()
	{
		anim = GetComponent<Animator>();
	}
	
	void FixedUpdate()
	{
	{
		rigidbody2D.velocity = movement;
	}
					
			float lastInputX = Input.GetAxis ("Horizontal");
			float lastInputY = Input.GetAxis ("Vertical");
		
					if (lastInputX != 0 || lastInputY != 0) {
					anim.SetBool ("walking", true);
			
						
					if (lastInputX > 0) {
							anim.SetFloat ("LastMoveX", 1f);
					} else if (lastInputX < 0) {
							anim.SetFloat ("LastMoveX", -1f);
					} else {
							anim.SetFloat ("LastMoveX", 0f);
					}
			
					if (lastInputY > 0) {
							anim.SetFloat ("LastMoveY", 1f);
					} else if (lastInputY < 0) {
							anim.SetFloat ("LastMoveY", -1f);
					} else {
							anim.SetFloat ("LastMoveY", 0f);
					}
			

			} else {
					anim.SetBool ("walking", false);
			}
	}

	void Update ()

{

	float inputX = Input.GetAxis("Horizontal");
	float inputY = Input.GetAxis("Vertical");
	

	movement = new Vector2(
		speed.x * inputX,
		speed.y * inputY);

		
		anim.SetFloat ("SpeedX", inputX);
		anim.SetFloat ("SpeedY", inputY);
		
				}
}

Later use the blend tree DOUBLE CLICK IN ANIMATOR STATE, is very easy. Look this for the blend tree.

I hope that this help you. Sorry for my english.

Hey, sorry but would you know how to do the same thing but instead of using the key arrows to move I want to use click to mouse

I want to move my car exactly like an android game 2 cars. any script ? please help