- Home /
Unity 2D My animator won't transition between states? Any help would be extremely helpful
Look i have been working on this 2d game nothing special I'm just trying to make my sprite walk, jump and attack. I've made the animations in unity with it's animation window but in the animator the states won't transition. I tested during the play and it's not responsive, it occurs randomly after i press the right key. I would like to know how i can solve it and or if it's just a problem with my code which i have feeling it is so please help and thank you for any feedback. My code
//these are the private types
private Animator anim; //contains animator value
private Transform pos;
//these are the variables
int hattack;
int hidle;
int hwalk;
int hrun;
int hjump;
int hleft;
bool facingleft;
public int Health;
// Use this for initialization
void Start () {
pos = GetComponent<Transform>();
anim = GetComponent<Animator> ();//allows variable to be called out
Health = 100;
hidle = Animator.StringToHash("Idle");
hwalk = Animator.StringToHash("Walk");
hrun = Animator.StringToHash("Run");
hjump = Animator.StringToHash("Jump");
hleft = Animator.StringToHash("Left");
hattack = Animator.StringToHash("Attack");
}
void Flip()
{
// Switch the way the player is labelled as facing
if (facingleft == false ) {
// Multiply the player's x local scale by -1
Vector3 theScale = pos.localScale;
theScale.x *= -1;
pos.localScale = theScale;
} else if (facingleft== true) {
Vector3 theScale = pos.localScale;
theScale.x *= 1;
pos.localScale = theScale;
}
}
// Update is called once per fram
void Update () {
//Seting input keys over here
if (Input.GetKeyDown(KeyCode.Space))
{
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Swing1"))
{
anim.SetTrigger(hattack);
}
}
if (Input.GetKeyDown(KeyCode.D))
{
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Idle"))
{
pos.position = new Vector3((pos.position.x + 1), pos.position.y, pos.position.z);
anim.SetBool(hidle, false);
anim.SetBool(hwalk, true);
}
/*facingleft = false;
Flip();*/
}
if ((Input.GetKeyDown(KeyCode.A)))
{
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Idle"))
{
facingleft = true;
anim.SetBool(hleft, true);
anim.SetBool(hwalk, true);
pos.position = new Vector3(pos.position.x - 1, pos.position.y, pos.position.z);
Flip();
anim.SetFloat("Speed", -1);
}
}
if ((Input.GetKeyDown(KeyCode.W)))
{
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Idle"))
{
pos.position = new Vector3(pos.position.x, pos.position.y + 1, pos.position.z);
anim.SetBool(hidle, false);
anim.SetBool(hjump, true);
}
}
else
{
anim.SetBool(hidle, true);
anim.SetBool(hrun, false);
anim.SetBool(hjump, false);
anim.SetBool(hwalk, false);
}
} }
Your answer
Follow this Question
Related Questions
Lighting system in pixel art 1 Answer
How to I create sudden movements using Unity's animator? 0 Answers
2D Animations Not Working After Building Game 1 Answer
Not playing animation after been hit by bullet 2 Answers
Left/Right movement 2D game,Why is this code to make my character move left/right not working? 0 Answers