- Home /
Animations Only Playing Once Per Key Press
I'm trying to animate the player character for a Unity game I'm working on. I have the different sprites for the animation, a sprite renderer for the player, and an animation program. However, when I run the game, if I press and hold down the move key, the player character will change its sprite to the next sprite in the animation and then stop changing sprites. If I release and then press the key down again, it will change its sprite, and again and again.
Basically, if I have Sprites 1-7 to cycle through for the animation, when I press and hold the move key down, the player will switch its sprite to Sprite 2, but not to any of the others. If I release the move key and then press it again, it will cycle to Sprite 3. If I release the move key and press it again, the player character will cycle to Sprite 4. If I hold the move key down while the player character is on Sprite 4, it will not cycle to Sprite 5. This is the only way it can cycle through the animation sprites. What am I doing wrong and how can I fix the issue so that the player character will fluidly cycle through the animation as long as the move key is pressed?
Here is the code for the animation:
using UnityEngine;
using System.Collections;
public class HorseAnim : MonoBehaviour {
public Sprite[] spritesRight;
//public Sprite[] spritesIdle;
public float framesPerSecond;
private SpriteRenderer spriteRendererRight;
// Use this for initialization
void Start () {
spriteRendererRight = renderer as SpriteRenderer;
}
// Update is called once per frame
void Update () {
while(Input.GetButtonDown("Horizontal"))
{
int index = (int)(Time.timeSinceLevelLoad * framesPerSecond);
index = index % spritesRight.Length;
spriteRendererRight.sprite = spritesRight[ index ];
break;
}
}
}
Your answer
Follow this Question
Related Questions
Trail of color behind the player 2D c# 1 Answer
Idle Animations, Player Walking 0 Answers
Create Player From Values [ Photon ] 0 Answers
How to move player after animation 2 Answers