- Home /
Input.GetAxis Animation Problem
I have been wrestling with this issue for a few days now and id really like some help to resolve it. I have a series of animations and for some reason one of them will not play. Here is my code and ill explain my problem after...
using UnityEngine;
using System.Collections;
public class AnimationTest : MonoBehaviour {
public float runSpeed;
public float gravity;
public int lookDirection = 1; // 1 is right, 0 is left
Vector2 velocity = Vector2.zero;
tk2dAnimatedSprite anim; //2dToolkit Animated Sprite
void Start ()
{
anim = GetComponent<tk2dAnimatedSprite>();
}
void Update ()
{
CharacterController player = GetComponent < CharacterController > ();
if (player.isGrounded)
{
velocity = new Vector2 (Input.GetAxis ("Horizontal"),0);
IdleAndRun();
Crouch();
}
WhereAmILooking(); //controls lookDirection
velocity.y -= gravity * Time.deltaTime;
player.Move (velocity * Time.deltaTime);
}
void IdleAndRun ()
{
if (velocity.x == 0 && lookDirection == 1)
{
if (!anim.IsPlaying("Idle"))anim.Play("Idle");
anim.animationCompleteDelegate = null;
print ( "Idle Right" );
}
if (velocity.x == 0 && lookDirection == 0)
{
if (!anim.IsPlaying("Idle Left"))anim.Play("Idle Left");
anim.animationCompleteDelegate = null;
print ( "Idle Left" );
}
if (velocity.x > 0 && lookDirection == 1)
{
if (!anim.IsPlaying("Run"))anim.Play("Run");
anim.animationCompleteDelegate = null;
velocity *= runSpeed;
print ( "Running Right" );
}
if (velocity.x < 0 && lookDirection == 0)
{
if (!anim.IsPlaying("Run Left"))anim.Play("Run Left");
anim.animationCompleteDelegate = null;
velocity *= runSpeed;
print ( "Running Left" );
}
}
void Crouch ()
{
if (velocity.x == 0 && lookDirection == 1 && Input.GetAxis ( "Vertical") < 0)
{
if (!anim.IsPlaying ("Crouch"))anim.Play("Crouch");
anim.animationCompleteDelegate = null;
print ("Crouching");
}
if (velocity.x == 0 && lookDirection == 0 && Input.GetAxis ( "Vertical") < 0)
{
if (!anim.IsPlaying ("Crouch Left"))anim.Play("Crouch Left");
anim.animationCompleteDelegate = null;
print ("Crouching Left");
}
}
void WhereAmILooking ()
{
if (velocity.x > 0)
{
lookDirection = 1;
}
if (velocity.x < 0)
{
lookDirection = 0;
}
}
}
My problem is that the 'idle' and 'run' animations in both directions work just fine. The 'crouch' animation, however, halts at the first frame. If i replace either the 'idle' or 'run' animation with a 'crouch' animation, the 'crouch' animation will play. Now, if i replace the 'crouch' animation with a 'run' or 'idle', they will not play but halt on the first frame. My problem has to be my conditions for calling this animation. What i have found is that if the player is grounded, it will only play animation that are controlled by the right or left arrows. I have tried all combinations of Input.GetKey, Input.GetButton, and Input.GetAxis and none of these methods work. Where am i going wrong? Why will the animations only play with the right or left arrow?
I'm pretty new to unity so i feel like i must be missing something simple. Thank you to anyone who can help me out.