Animate like a Programmer
Vložit
- čas přidán 31. 05. 2024
- As a programmer, using the Unity animator can feel like you're losing some control over your game. We can manage our animation transitions entirely from code... Animation will feel as natural as your player controller script.
Code: bit.ly/3P4DdA2
I'll be expanding this code on my ultimate 2D controller, which can be found here: / tarodev
Animancer: assetstore.unity.com/packages...
❤️ Become a Tarobro on Patreon: / tarodev
=========
🔔 SUBSCRIBE: bit.ly/3eqG1Z6
🗨️ DISCORD: / discord
✅ MORE TUTORIALS: / tarodev
I wasn't convinced until I saw I could bypass trying to line up the animator arrows in the grid
Exactly!
hahaha
I'm comforted knowing I'm not the only one driven crazy by this. I also spend way more time in Shader Graph lining up nodes than I do making actual shaders...
this saves me soooooo muuuuuuuchhhh tiiiime i love this video
@@blameyourm8519😊p . ;blue🔵 h😊) gF/+0=°Phl de₹
As a programmer learning game making in Unity I really like this. Now, if only there was a "How to create art/animations as a programmer"
Oh mate... If you find it, let me know
You can add StateMaschineBehaviours to your Animator states. That way you can programmatically create animations.
It is a bit finiky tho. f.E. the duration of the animation is set by the animation set in the state. There is a default duration you can just repeat or you just add a empty animation with your desired duration or you just use your animation and use it to spice up that animation.
Plus there is no transitioning, what makes the creation of good animations hard.
Transforms, you want to change, must not have any data in the animation you set.
In general I would use this feature for simple animations like idle bobing or for stuff that has the same start and end point like firing a weapon.
I personally believe it wasn't really designed for that.
Sadly I haven't found a good package, that adds new features for such an use-case.
Program a bot to scrape assess from pre existing games
Asset store?
Dalle 2?
I love how this video is under 2 minutes. Quick and straight to the point. Personally, I prefer the tree and have spent a great amount of time mastering it, but for quick prototypes and game jams this would be WAY faster to set up compared to messing with the tree.
Glad you enjoyed the short form content brother 🙏
What do you prefer about the graph/tree? I've only made a couple small projects in Unity, but this approach seems much more convenient to me. Would it be a problem with scale?
@@Glooberloob The strength of the graph/tree I believe would be helpful for 3D games such as combo-ing and whatnot. Also, the Finite State Machine Behavior can be more easily used in conjunction with the graph/tree (I think, correct me if I'm wrong but I find it easier to fix there especially with AI)
I've only found your channel yesterday but already you are in my top 5, short and to the point. Keep up the good work!
Aiming for that #1 spot
@@Tarodev Im sure you'll get there in no time 😉
@@Tarodev You'll get there for sure, i love the fact that you cover "intermediate" / "advanced" topics as well that don't necessarily bring you as many views but are much more useful than having 1000 + 1 character controller tutorials
Come hang out in his discord!
Which channels are currently in the top 5?
I am working on animations literally this second. God sent you
"I read every single one of them."
WELL THEN, I HAVE SOMETHING TO SAY TO YOU.
that's a very well done video and I appreciate the time and effort that it must have taken to make it.
Oh... Well thank you sir
More tutorials need to be like this, immediately explaining what you want to show off without fluff. Amazing vid
I always appreciate seeing new approaches to the same repetitive things I've been doing all the time.
Thanks mate🐦
Thank you so much for posting videos for intermediate level coding, it’s just what I need!
This is so interesting. I would seriously love to learn more about how to implement this. It seems so clean and efficient. Every 2D tutorial I’ve seen just talks about the regular ‘spiderweb’ way.
You seriously limit yourself if you animate this way. However, you shouldn't have spiderwebs in the animation graphs either. Once you learn the animation graphs system you will have clean clear animation states using layers, substates, and blend trees. the spiderweb only comes from lack of experience. animating in code as shown in this video is not recommended.
“Clean”? This literally leads to ridiculous repetition and duplication in your code. It’s the opposite of clean.
Have been programming in Unity for over 6 years and always wrote my own animation system to avoid the issues addressed in this video.
I never heard of CrossFade, this would have solved everything I solved writing my own systems while keeping it connected to the tools Unity Provided. Thank you for showing this!
To return the favor, let me address the following thing I noticed about the video:
About the animation states. I categorize finite state machines into 3 categories:
Level 1: Tracking state by multiple booleans
Level 2: Tracking state by an enum
Level 3: Tracking state / having the logics of states in separate classes.
You were using a Level 1 Finite State Machine. I highly recommend going for a Level 2 or Level 3 Finite State Machine.
The nice thing about having it in separate classes is that you can do simple things such as:
* WalkingStateClass (holds logics of walking)
* FreezeStateClass (changes color when entered, and back when exited)
And to have the player freeze when hit by an object, you only have to:
CurrentState.Exit();
CurrentState = new FreezeStateClass();
CurrentState.Enter();
Because WalkingStateClass is no longer CurrentState, it no longer is being updated so no walking logics is available anymore. Keeping your behaviour simple and preventing complicated bugs.
Very interesting. Thanks for the write up 🙏
I really love your videos man, short and to the point. I’m a little confused on 3D animation but honestly the more I think about how to implement it the more I think I can
If anything 3D animation is a bit easier as you can interpolate frames between two animations (this is handled automatically) to create smooth transitions. You can do it brother!
had to come back with a new channel because the game i was working on last year uses your character controller... thank you for existing btw! hope you're overcoming any stress you're dealing with at this current point in time and that this year goes great for you :)
+1 for Animancer Pro, it makes working with animations programmatically so much easier. And, even solves some awful blendtree glitchiness by letting you disable foot IK on a per-animation basis. Technically, another option is directly implementing the somewhat abandoned Playables API which Animancer uses behind the scenes.
Yo, that was extremely helpful, I saw a video from lost relic games talking about the concept of coding animations, but you took it to the next level
Thank you! I didn't know about this feature! I already have a state machine in code to manage transitions and trying to match the Unity animator with it was painful. This just works with basically one line of code! I think about this video almost daily and feel thankful for it :)
I switched to code-driven animation almost from day 1 of learning Unity after seeing LRG's tutorial on how to avoid Mekanim spiderweb madness. I barely even know how to use the Unity animator at this point. Your string hash to int trick is something I have not been doing though, until now! Thanks!
Got a link to that video?
@@gamedevteacher7065 czcams.com/video/nBkiSJ5z-hE/video.html&ab_channel=LostRelicGames
Simple little string based animator state machine. I've extended it a bit for personal use and even implemented parts of yours, but John's does a fine job even just out-of-the-box
This is sooo handy! Definitely going to play around with this. Thank you!
You can take this a step further by using Scriptable Objects to create states and then having a State Machine Monobehaviour. This way you can link up states to different animations which is particularly handy for games with multiple playable characters and generally works better as a more modular methodology than the firewall
I actually love that!
Im getting into unity with some friends. Im a professional programmer but this is my first real foray into game dev. My friends are all artists so I handle all the code. This channel is everything I want. I can figure out how to do nearly everything from the docs but never really know if Im following good practice. Most tutorials have been worthless because theyre not written for or by programmers. Loving you channel because its exactly what Ive been looking for
I'm here for you 🙏 enjoy your journey
Really like this idea tbh. In the past years, for a few of my games were animation transitions aren't that relevant, I always just made a state machine that is used in the MonoBehaviour and just play and adjust animation state in the logical states, kinda like you showed in the video. It prevents a lot of the pains of having to deal with the Animator and having to maintain 2 different state machines. More recently I also worked on a small editor script that basically generates the hashes of the animator state names and parameters as static variables into a file, that's pretty useful as well ^^
I really love your videos, they always have interesting topics actually.
Wow, that looks a lot easier to follow than the spider-webs with parameters that you can't tell apart. I'll also have to look into that string hash thing you used; never heard of that one.
😭 the line… you just touched my heart. That line drove me insane for so long. I hate it.
This Channels like finding a hidden gem, informative and quick to the point. Nice content
Great video! I'm glad to see more devs promoting this approach. The mechanim is a clunky and unessessary system (for coders).
You know, I've watched most of your videos, but somehow missed the one on this topic. Another commenter mentioned they originally learned it from you, so I went and watched it swiftly 😂
Love your stuff man!
This tip is so simple yet so effective. It just pulled me out of a full half-day unity animator nightmare. Thanks Tarodev 💙
So much information condensed into a video that's under 2 minutes. Beautiful Taro! And now it feel like a ton of my videos should be refactored to use this method of animating XD
Combining this with the state machine that you showed us = gold!
maybe you make a video about this.would be good.
Simple, objective and precise.
Thank you Tarodev!
I work as a software developer and I'm just starting to get into game development in my free time. This was perfect for me as I prefer to use code over Unity tools if it makes the process simpler, which this really does. Thank you!
This is absolutely fantastic, thank you so much! I love doing things through scripting, it just makes so much more sense in my head, and I feel like I have so much more control over how I do things. Never going back to spaghetti trees lol.
Good job making this video, I feel the YT algorithm read me my mind and showed me this when I needed it.
What an amazing video, this is so much better than trying to fiddle with the unity ui
Amazing idea. Animated transitions have not been relevant for some of my games over the past few years. I just created a state machine for the MonoBehaviour and just played and adjusted the animation state in the logic state like you showed in the video. It avoids many of the problems of dealing with animators and having to maintain two different state machines.
I'm relieved that I'm not alone to align horizontally the animator connection lines :D
This is super useful! More of this please :)
I'd like to understand ANYTHING you just said. Well done mate.
I love your videos, you just get the point so efficiently. thanks.
thank you! This reminded me of a tutorial i watched last summer from Kiwicoder but in 3D. Programming animations this way seems the cleanest way.
Good stuff, I use a very similar system for a 2.5D platformer project, the level of control is very good to have, and no animation/transition setup overhead.
LockState is probably what I needed. I followed LostRelics version of this in a lil endless runner I made and I noticed animations being overridden. So in the animator I linked necessary ones back to idle, also turning off the autoloop, and then in my if statement checking player states, I stop/prevent the state changing unless it's not one of those animation states. E.g. player is landing from a jump, even if they are grounded, I don't want idle to play since I want the jump animation to finish, if not jumping play idle but when jump is finished in the animator it will go back to idle. Your LockState example essentially does what I wanted 👌
Also I wish beginners knew of this because Unitys spider web is a nightmare and I can't see an advantage over just coding it.
Bookmarking this for the future as i think ill never use the built in animator window ever again
thank you so much for sharing this. it seems like a much easier way to animate considering i always just set up animation triggers and call them one by one. and great presentation too!
Enjoy your new power
this is absolutely awesome did not know about this, thanks !
Good stuff! My current game is a track- based music game so all of my character animating is being done on the timeline for this one, but I'll definitely keep this in my bag of tricks for future projects. The more you can calculate programatically, the better!
This strategy seems ideal for that type of game, so long as you have events being fired for the beats
@@Tarodev Actually, that makes sense. I was just going to make manually animated segments with timeline triggers to branch/loop, but that could potentially help automate at least some of the animation setup. I'll have to weigh my options!
Always love your stuff. Best unity tut tuber
Thanks Caleb 😘
This is amazing! Getting rid of transitions and blend trees is a god send. I work exclusively with pixel art games and this is a perfect solution to the spider web woes I continue to deal with. This method is simple and intuitive. As a newb to game dev, a more detailed video doing a deep dive into creating the script and going through some different interactions of the code with various actions like attacking, jumping, et cetera, would be great. Thanks for the code and the quick video on how it all works.
I'd love to hear how you go with it. Reply here when you've had a play 😊
I have a pretty fleshed out version on my patreon if you're ever interested
This is so helpful. As someone coming to Unity from GameMaker Studio, this is really similar to how animation is done there. So much easier for sprite work!
I wasn't aware of that, interesting
this looks a lot easier than working with unity animator, it would be great to see a full tutorial on this!
Yes - an explanation of your full code, and what connects to what components, what terms or commands actually do, why you use the techniques and code you use, etc - that'd be very useful to us folk just starting out!
This is marvelous
Good luck brother! Remember, the provided script is still basic. You can deck it out with some advanced features like OnFinish and OnStart events
@@Tarodev I have a state machine based character controller so I already have all the transitions mapped out! Thinking of switching it to coyote based state machines so I can decorate the states with attributes on what animation to switch to when loaded!
I highly recommend using Playables graphs for 3d animation. It's built into unity (but with little documentation) and offers even more programmer centric design. For instance, you can use animation clips as serialized fields and reuse animation graphs just like you reuse code.
There is extra work needed to get it set up but it gives you so much more control. It also let's you customize transitions so they're not linear!
Animancer takes away most of the extra work and uses playables in the backend.
Crossfade is new to me, this seems right up my alley. Great video man 👊👊
Mr Taro, that is fantastic video! Thank you so much for it :D. I had no idea this method exists!
Thanks buddy :)
This was really helpful, I'm just not a big fan of the unity animator, and my brain works very logically, so I will be using this method a lot more, thanks so much!
Amazing work, bro !
This was helpful and motivating, thanks.
this channel is a blessing ..honestly. superb content.
Thank you ❤️
You had me at OCD, really enjoying your vids man keep it up PLEASE. Learning lots even started making my own space shooter!
Hell yeah brother!! Good luck with the shooter
I just know this API after a few years working with unity, thank you this is looks awesome.
Hey TaroDev! Great video, honestly struggled with animations and the damn spider webs way too much. Actually checked you out after the blackthornprod 3d game dev video, you were a complete machine in that! Thanks for the video mate, have a good one!
Welcome aboard mate 😊
I've had a bunch of complaints that I threw out too many previous dev implementation in that video, lol.
Woops.
@@Tarodev all good mate, p.s I liked your changes
Didn't expect that 2 minutes video could be so useful.. You are genius, thanks!🙂
Keep the good vids, amazing!
Oh man! Thank you so much... I love this!
Thank you. I converting the state to hash looks like a good idea and it could be used for an enum. Can't wait to test it out.
Just ensure you're still sending the hash to the animator as it's significantly more efficient
I always had so much trouble with animator in unity. This video is great!
This is sooo useful, thank you sir
What an effective, quick video.
Humour, bit size and taught well.
Thanks Eli
It's like you were sitting across the "office" from me today! I've been struggling the past week with this exact scenario but 3D. Thank you so much sir.
Here to help 😉
Great video Taro! Never heard of CrossFade or the firewall pattern before this, definitely have to try them out
You've never heard of the firewall pattern as I coined it while writing the script :D
@@Tarodev Well now I don't feel too out of the loop :D
The standard name for the "firewall pattern" is "guard clauses". It normally refers to returning after checking function parameters for valid values, but can also refer to a string of conditionals and return statements. Firewall pattern sounds cooler, but it might be a confusing term if you're writing network code.
Another great video! Can we have your take on creating an inventory system? I know this has been done many times before but I love how simple you make complex topics and I'm sure you'll do a fantastic job with inventory. Covering things such as; Setting up the inventory, drag and drop functions on it, reactive UI based on what you have picked up / dropped and maybe some extras such as combining items from that inventory (for example, crafting.) Because these things are used in many games.
Incredible content!!! Feel like a beginner watching your videos!!! Grow that channel!! Grow like crazy!!!
I'm tryyyyyyiiiinnnnngggg 😂
"Tell me anything" you say?
Well, the mitochondria is the powerhouse of the cell.
CELLS AT WORK!!
I've been looking for something like this!
Awesome video Taro!
Would you mind giving some insight on the "Cached Properties" ? I understand the goal is to calculate them once and store them, but by making them static im not sure when it would be computed (during creation of the first object of that class?)
This is so helpful!!
Thank you man 🖤
I thought this was an asset you made and wanted to show off but no, this is straight up base unity functions. How has this evaded me for so long?!?!
It evaded many of us... But we now have the power.
@@Tarodev I will follow you to the ends of the earth, finder of ancient treasures
How would you override a locomotion blends idle/walking animations in this method? E.g. depending on the weapon the char holds or how tired he is?
Would you override animations at all?
Brilliant stuff
Probably outside of the scope of a quick vid like this, but if you're using a state machine pattern within your own code, changing animations like this can easily be handled more elegantly than a block of if statements, ESPECIALLY if your animation is tied to state.
Just have some sort of "onStateEnter" function on your states where you set your animation.
I use state machines all the time, but I like my current approach more. Way (way) less boilerplate, exact same result.
Great job as always! by the way thanks to you that you made me understand i have OCD
Yoooo 🤯 this is literally exactly what I need after just deciding today to make my platformer 2.5D with models instead of 2D with the same models baked as sprites. Nice 🤝🏾
I love 2.5D. Good luck with your project!
@@Tarodev Thanks, good luck with your projects as well 👍🏾
That's an easy sub for sure. Gonna try this sometime soon
dude that perfect thank you!
Brilliant!
Hi, nice tutorial, very well done.
Can i ask why you use the CrossFade() function rather than the Play() function, especially if you don't use transition time?
Thanks.
excellent vid!
I gotta test this one
Omg this is everything I ever needed as a programmer. Thank you
Enjoooooy
how do you bump the player up when he undershoots a jump in a platformer?
Do you think i can still transition to that animation approach in a big project? Im handling quite a bit in the animator.
I basically run the the state machine in the animator but transitions to hit and death animations are always so clunky, that the combat feel really suffers I feel.
If you’d transition to the programmer approach, would you implement something else than a switch-case based FSM?
I believe this would scale quite well. I did it as a decoupled script using an interface, but you can place this logic directly within your controller code, which would reduce boilerplate by quite a lot.
but again, if you implement a FSM for handling states, you are already doing what Unity did in the first place. I think you can also watch a gdc video called "Animator Hell" where a developer gives tips and tricks for handling lots of animations.
I think when it's understood FSM approach of Unity is quite nice. Just pay attention to the layers, blend trees and state trees(I guess? you can put multiple states inside for organizing)
for exp: you can use blend trees for different attacks and you would transition between attack animating blend value, then you can specify time it takes to blend into that value, that way you could control the transition speed.
ooh, nice man. Thanks for that. I both hate and love unitys animation tools. But this will help me a bunch!
YOU MY MAN ARE SPEAKING MY LANGUAGE
you are a fooking legend!
Thank you so much!
Wowwww i wanted to know this before, really thank you
Animate like a programmer is such a powerful sentence. Draw like a skilled math teacher
Why do mathematicians always have such elegant handwriting? 10k hours I suppose...
@@Tarodev skill based
wow thought this is a tool or something like that, but the animator itself has it. how did I not see this for all these years :D
this is super helpful thanks 🙏
Great content as always, one of the best Unity and c# channels. I discovered Crossfade lately and started using it, a great tool, way better then the graphical setup.
Will you eve do a Fish Net Overview / Tuto or any content related to it ?
I may do a NetCode/FishNet comparison video, but won't do a full blown FishNet tutorial. If you're after related content, checkout my recent NetCode video.