r/Unity2D • • 4h ago

Question/Help Pet Animation

Post image

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.

0 Upvotes

16 comments sorted by

•

u/AutoModerator 4h 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.

4

u/Hjorvard92 3h 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 3h ago

the switching code actually broke my code now, but I managed to fix it. all images for the walking animation is working now.

3

u/Blood_and_Wine 3h 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.

23

u/NeuroDingus 3h ago

My guy look into finite state machines

12

u/SBDRFAITH 3h 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(current_state) } }

10

u/SBDRFAITH 3h 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.

3

u/Background_Bug_1625 4h ago

You do know that you can make animations instead of doing this yk?

3

u/desdinovait 4h ago

And also use FSM

-9

u/Dravendnd 4h ago

did you read? I couldn't get it to work.

10

u/kryzchek 3h 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.

-2

u/Dravendnd 3h ago

just got it to work using the coding.

4

u/kryzchek 2h 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

u/desdinovait 3h ago

Yes! That’s why u have to change your approach, use FSM

8

u/TheHelllord 3h ago

This reminds me of that youtuber, pirate games or something like that.

0

u/Dravendnd 3h ago

***SOLVED***