- Home /
How can I cycle through sprites on button presses (C#)?
Hello!
I am working on a top down, 8 directional movement where the player is constantly moving forward, and pressing left and right arrow keys rotate the player 45 degrees and change the sprite to a new one matching the new direction. How can the sprite cycle be done? I've tried true/false values but they cycle through all at once without stopping, and I've tried floats but I can't add a value to a float that applies to the animator for some reason. Pressing right or left should cause a sprite to switch just once, and pressing again would switch to a new sprite, while pressing left would bring back the previous sprite. I'm completely new to C# so any advice will help.
Thanks!!
Answer by JayOhhh · May 30, 2014 at 03:57 PM
I would suggest using an animator and a sprite sheet. You are going to want to load all of your 8 sprites into a sprite sheet and then import that into Unity. Once imported you are going to want to create a state for each sprite direction. Set a variable to control which state you want to fire on button press to change the sprite.
Thanks! I've done all that, and I'm able to do 8 directional movement based on combinations of key presses, but what I want eventually is to use just Left and Right keys to cycle through the different sprites, which I can't seem to figure out
Your answer
![](https://koobas.hobune.stream/wayback/20220613150019im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Start sprite animation at the same index that the previous animation stopped 2 Answers
Sprite image not changing 0 Answers
Way to have 2D Sprites lit by Lights? 3 Answers
Strange artifacts in builds 0 Answers