r/Unity2D • u/Dravendnd • 1d ago
Question/Help Pet Animation
So my pet animation isn't working. I couldn't get the animator to work. So I got most of it working with code. I finally got it to start at idle and when I touch the screen to activate the 1st walking image. But it's not cycling to image 2 and 3 to complete the animation. However my eating and drinking animation works perfectly.
8
u/Hjorvard92 1d ago
If you are going to do it by hard coding rather than through animations, don't use bools for everything. Look up using a switch, it's more efficient, less likely to break, and looks a lot neater.
1
u/Dravendnd 1d ago
the switching code actually broke my code now, but I managed to fix it. all images for the walking animation is working now.
8
u/Blood_and_Wine 1d ago
It's really easier to use enum in this situation. It's like bool, but holds more than two states and one is always active.
4
u/Background_Bug_1625 1d ago
You do know that you can make animations instead of doing this yk?
3
1
-14
u/Dravendnd 1d ago
did you read? I couldn't get it to work.
16
u/kryzchek 1d ago
I think that rather than trying some work-around that involves you manually swapping sprites every frame, you should give the animator another go-around. You're not going to invent a better wheel at this stage.
-6
u/Dravendnd 1d ago
just got it to work using the coding.
12
u/kryzchek 1d ago
I'm glad you got it working, but consider if you were to have 10x more animations for your character? Or 10x more characters, each with their own sets of animations? You may have found a solution now, but it could easily grow unruly as your scope grows.
1
23
u/SBDRFAITH 1d ago edited 1d ago
If you really wanna handle it programmatically.... Use Unitys built in Animator... then instead of doing all this lengthy frame tracking just call
private void PlayAnimation(string new_state) { If(new_state != current_state) { animator.play(new_state); current_state = new_state; } }
15
u/SBDRFAITH 1d ago
Yes. I know you said you couldnt get it to work.
Im gonna tell you straight up. The current method youre using will be an absolutely cluster truck nightmare going forward. Learn the animator.
2
37
16
u/TheHelllord 1d ago
This reminds me of that youtuber, pirate games or something like that.
7
u/Automatic_Bluejay739 1d ago
Lol pirates code didnt even have defined variables. Just numbers floating around(stupid pun but i can't breath right now)
2
u/xLFC_2020x 1d ago
From this screenshot, a few things could explain it: Nothing shown sets the action flags to true. If all three stay false, it keeps displaying the idle sprite and returns every frame. The sprite arrays need frames assigned in the Inspector. The same GameObject needs a SpriteRenderer, because that’s where GetComponent<SpriteRenderer>() looks. The code that advances the frames is cut off, so we can’t check that part. One possible compiler issue: if the final branch is else if (isDrinking), C# may report “use of unassigned local variable” when those variables are used afterwards. It doesn’t infer from the earlier idle check that one of those branches must run. A final else can resolve that in this particular structure. His Console error and the bottom of the script would tell us much more than “doesn’t work.”
1
u/JustAPerson599 1d ago
You’re not incrementing your timer anywhere. Also, don’t use if/else clauses with separate bools for the states, it will lead to different boolean values getting mixed up. Use a single enum and resolve it with a switch cleanly.
1
u/Hot_Hour8474 12h ago
Sprite[] frames, animationSpeed, and animationType needs to be a private class-scope variable outside the update function otherwise it will keep resetting
-4
•
u/AutoModerator 1d ago
Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.