- Home /
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...
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.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
rigidbody2D.AddForce(Vector2.up * v * moveForce);
// 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
Answer by Pyrian · Apr 20, 2014 at 11:35 PM
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.
I see. So do you know how my code should look, or were I may go for a tutorial?
I'd have to see your Animator transitions to know for sure, but I think if you just remove the $$anonymous$$athf.Abs functions on lines 34-35, passing in h & v unmodified, you might be golden. Those SetFloat's are the only calls to your Animator, and the $$anonymous$$athf.Abs is certainly killing any directional information from getting through.
Ah ha! Eureka! I figures It out thanks to your observation and I will post my new script and a description of the solution. Thank you for the help! ^_^
Answer by coolbird22 · Apr 20, 2014 at 05:42 AM
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.
Thanks for the recommendation, I will look into that ^-^
Answer by Grendel_Demon · Apr 22, 2014 at 02:14 AM
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.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
rigidbody2D.AddForce(Vector2.up * v * moveForce);
// 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);
}
}