# How to play correct animation based on 2d movement?

Hi all, I am making a 2d side scrolling shooter, and have a problem with linking my thruster animation with the ships forward, back, up and down movement. I have watched several tutorials, but all have only shown me how to move the player by the “vertical/Horizontal translation”, not specifically “up” and “down”, “left” and “right”. Below is my player script, I am coding in C#, and an illustration of my problem…

[25492-movment+problem.jpg|25492]

``````using UnityEngine;

public class PlayerScript : MonoBehaviour
{

public bool facingRight = true;			// For determining which way the player is currently facing.

public float moveForce = 40f;			// Amount of force added to move the player left and right.
public float maxSpeed = 5f;				// The fastest the player can travel in the x axis.

Animator  anim;

void Start ()
{
anim = GetComponent<Animator>();

}

void Update()

{

}

void FixedUpdate()
{
// Cache the horizontal input.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");

// The Speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat("Speed", Mathf.Abs(h));
anim.SetFloat("Up", Mathf.Abs(v));

// If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...

if(h * rigidbody2D.velocity.x < maxSpeed)
if(v * rigidbody2D.velocity.y < maxSpeed)
// ... add a force to the player.

// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)

// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);

}

}
``````

I hope I was concise in my explanation, and any advice would be most helpful :3

Try using this Java to C# converter - Java to C# converter
Do note that you might have to rename your variable type and/or classes.

The reason your animations work for right and up but not left or down is because you’re passing the absolute values of your input. This converts any left or down input into right or up input, respectively.

To fix my problem, I created 4 different animator parameters for the 4 directions I wont the ship to animate in. I then blended between the idle state using the appropriate float parameters. to link this all up, I changed my mathf.Abs to mathf.Min, thanks to Pyrian’s observation. I also had to update my new animator parameters in the script, and add a -v and -h parameter. Here is what my script looks like now;

``````using UnityEngine;

public class PlayerScript : MonoBehaviour
{

public bool facingRight = true;			// For determining which way the player is currently facing.

public float moveForce = 40f;			// Amount of force added to move the player left and right.
public float maxSpeed = 5f;				// The fastest the player can travel in the x axis.

Animator  anim;

void Start ()
{
anim = GetComponent<Animator>();

}

void Update()

{}

void FixedUpdate()
{
// Cache the horizontal input.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");

// The Speed animator parameter is no longer set to the absolute value of the horizontal or vertical input.
anim.SetFloat("Right", Mathf.Min (h));
anim.SetFloat("Up", Mathf.Min (v));
anim.SetFloat("Down", Mathf.Min (-v));
anim.SetFloat("Left", Mathf.Min (-h));

if(h * rigidbody2D.velocity.x < maxSpeed)
if(v * rigidbody2D.velocity.y < maxSpeed)
// ... add a force to the player.

// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)

// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);

}

}
``````