Can't seem to get my animations to work proper
So I'm working on a game that has the player movement in the style of a 2D RPG and I have walking animations setup for when the player is facing in each direction (up, down, left, right). I wrote my script and it make the player face in those directions but I want to make it so when you try to go diagonally it just does the animation for walking on the horizontal axis. So for example holding the up and left keys makes the player walk in the north-west direction but I want just the left walking animation to play rather than it rapidly switch between the up and left animations. I'm really stumped on how I should go about writing the code for this so it works how I want. Here is the script that I wrote for my character movement and playing animations:
using UnityEngine;
using System.Collections;
public class player_movement : MonoBehaviour {
[SerializeField]
private float speed;
private Animator playerAnimator;
private Rigidbody2D playerRigidbody;
void Start()
{
playerRigidbody = GetComponent<Rigidbody2D>();
playerAnimator = GetComponent<Animator>();
}
void FixedUpdate()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
HandleMovement(horizontal, vertical);
Facing(vertical, horizontal);
}
void HandleMovement(float horizontal, float vertical)
{
playerRigidbody.velocity = new Vector2(horizontal * speed, vertical * speed);
}
private void Facing(float vertical, float horizontal)
{
if (vertical > 0.1){
playerAnimator.SetFloat("up", 1);
}
else{
playerAnimator.SetFloat("up", 0);
}
if (vertical < -0.1){
playerAnimator.SetFloat("down", 1);
}
else{
playerAnimator.SetFloat("down", 0);
}
if (horizontal > 0.1)
{
playerAnimator.SetFloat("right", 1);
}
else {
playerAnimator.SetFloat("right", 0);
}
if (horizontal < -0.1)
{
playerAnimator.SetFloat("left", 1);
}
else {
playerAnimator.SetFloat("left", 0);
}
}
}
If someone could tell me how I should be going about this I would really appreciate it. I'm just getting started with scripting and I'm still really new.
Answer by Abhiroop-Tandon · Mar 24, 2016 at 10:42 AM
try this instead and let me know if it works or not
private void Facing(float vertical, float horizontal)
{
if (horizontal > 0.1)
{
playerAnimator.SetFloat("right", 1);
playerAnimator.SetFloat("left", 0);
}
else if (horizontal < -0.1)
{
playerAnimator.SetFloat("left", 1);
playerAnimator.SetFloat("right", 0);
}
else{
if (vertical > 0.1){
playerAnimator.SetFloat("up", 1);
playerAnimator.SetFloat("down", 0);
}
if (vertical < -0.1){
playerAnimator.SetFloat("down", 1);
playerAnimator.SetFloat("up", 0);
}
}
}
I had high hopes but this doesn't work. Sorta just makes it rapidly switch between 2 animations after pressing any key combos or pressing the right key.
Basically if I press any two keys to move horizontally it just quickly switches between the facing animation for both directions. Other than that trying your code also made it so that some times just pressing the left key makes it do what I described in the last sentence.
Your answer
Follow this Question
Related Questions
How can i stop my shooting animation after playing? 0 Answers
How to immediately CHANGE SPEED from spotted position to another?) 1 Answer
2D Spine Animation not playing 0 Answers
Player should turn in the direction the player is running. (2D Game) 0 Answers
HELP,Watched tutorial https://www.youtube.com/watch?v=Xnyb2f6Qqzg 1 Answer