Animation transitions are not always triggered with state change
I have a simple character controller script which I use to use WASD to move a character forward, backward and side to side. I then have an animation script that takes the WASD button presses and declares an animation state (0 = idle, 1-4 = up,down,left,right) which then triggers animations related to those directions.
This works, but often the character animations will transition to idle instead of going to the transition that the state change should dictate.
What do you think is going wrong to create this inconsistency? Scripts and screenshots below.
Player Movement Script
using UnityEngine;
using System.Collections;
public class playerMobility : MonoBehaviour {
public float speed;
private Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D> ();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
rb2d.AddForce (movement * speed);
}
}
Player Animation Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerManager : MonoBehaviour {
Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.W)) {
anim.SetInteger("State",4);
}
if (Input.GetKeyUp (KeyCode.W)) {
anim.SetInteger("State",0);
}
if (Input.GetKeyDown (KeyCode.S)) {
anim.SetInteger ("State",1);
}
if (Input.GetKeyUp (KeyCode.S)) {
anim.SetInteger ("State",0);
}
if (Input.GetKeyDown (KeyCode.D)) {
anim.SetInteger ("State",3);
}
if (Input.GetKeyUp (KeyCode.D)) {
anim.SetInteger ("State",0);
}
if (Input.GetKeyDown (KeyCode.A)) {
anim.SetInteger ("State", 2);
}
if (Input.GetKeyUp (KeyCode.A)) {
anim.SetInteger ("State", 0);
}
}
}
Animator screenshot
,
Your answer
![](https://koobas.hobune.stream/wayback/20220612151636im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D anims with same animation but with different sprites. Character creation. 0 Answers
Button Animation Transition Doesn't Work 0 Answers
Combining Multiple Sprites 0 Answers
Animation Script on GetKeyPressed 0 Answers
2D 8-way sprite animation 1 Answer