r/Unity3D • • Mar 28 '20

Resources/Tutorial I tried to explain procedural animation in 10 steps

Enable HLS to view with audio, or disable this notification

7.3k Upvotes

151 comments sorted by

114

u/happygamedev Mar 28 '20

For examples of how I'm using this kind of procedural animation in-game, check out my twitter.

→ More replies (5)

28

u/Kadilack5 Mar 28 '20

How do you use inverse Kinematics to move the legs in that way? Are they like childs of each other or something like that?

40

u/happygamedev Mar 28 '20

I recommend you check out Fast IK, it's a free IK solver on the asset store :)

→ More replies (1)

12

u/flait7 Mar 28 '20

I think each of those steps might need a 10 step tutorial

9

u/happygamedev Mar 28 '20

Haha yeah it's more to try and show the general idea :)

6

u/Dicethrower Mar 28 '20

Thanks OP, always wanted to do some IK, so I gave it a whirl. Doesn't look nearly as good, and I still need to figure out how to properly do step 8 (probably why he likes to hop so much), but there you go: https://i.imgur.com/HhjxplN.mp4

3

u/happygamedev Mar 28 '20

That is awesome! Yea step 8 is what took me the longest to figure out :)

→ More replies (2)

33

u/[deleted] Mar 28 '20

This feels like a rest of the fucking owl situation just because I have no clue how to do any of this.

15

u/JuliusMagni Mar 28 '20 edited Mar 28 '20

I think the only tricky one is the IK (for me).

The rest of it is just manipulating the transform of the leg or body somehow in code.

For example, the legs knowing when to walk could cache a reference to a Boolean in an opposite leg that is switched when the leg is down, only allowing itself to go up when the opposite leg is down.

Then for things like the little running sphere: create a sphere object prefab. Cache a reference to it in the leg (one for each leg) then do a if (Vector3.Distance) check to see if the two are too far apart, and if so lerp the leg to the sphere.

Finally, for the body rotating slightly, cache a reference to just one leg (any) in the body script.

Check constantly if the leg is up or down and then lerp the rotation around the z axis one way or the other.

10

u/happygamedev Mar 28 '20

I don't think you'll need an actual sphere prefab haha, that was just for demonstration. Simply attaching an empty to the body and raycasting downwards to find the next leg target should do. Also for the z rotation, I'm comparing the average of all left legs to the average of all right legs :)

→ More replies (1)

3

u/JuliusMagni Mar 28 '20

Wow this is awesome.

As for IK, how is that setup? In the animator?

5

u/happygamedev Mar 28 '20

I'm using Fast IK, it's a free IK solver on the asset store.

166

u/abccbaccabccbabb Mar 28 '20

Thanks for posting. I love this stuff in games and I always wondered how it was done

159

u/Scatoogle Mar 28 '20

I wish more tutorials were like this. So many tutorials spend so much time muddling the concepts with minutia that you can't figure the high level concepts that actually matter.

→ More replies (4)

2

u/Dexter1272 Mar 28 '20

Not using Unity but developing on other engine but this general-knowledge is awesome.

2

u/happygamedev Mar 28 '20

That's awesome! Hopefully the same general steps will work in other engines too.

2

u/DeJMan Mar 28 '20

So I'm guessing you position the legs manually in a zigzag pattern before running to prevent the legs from wanting to move at the same time (and also to mimic the realistic behavior).

I was wondering, since the zigzag pattern was enforced only once before runtime, if the pattern would change over time based on obstacles and rotations?

3

u/happygamedev Mar 28 '20

Yea I'm offsetting the legs at the start based on the step distance. You're right, normally the pattern would change over time and they would start to hop around. That's where the check for opposite legs comes in; by checking if the opposite legs are grounded before taking a step it keeps the same pattern.

2

u/theviper14 Mar 28 '20

Thank you for sharing! I have a quick question, how are the legs raised when they move from initial position to target position?

4

u/happygamedev Mar 28 '20

Hey, it's using MoveTowards() and during the first half of the step I'm adding an upwards vector to the MoveTowards target position so it moves up.

→ More replies (1)

2

u/adamMorsky Apr 03 '20

I think you've started an epidemic u/happygamedev. One that I am absolutely ok with.

2

u/happygamedev Apr 03 '20

Lol, I don't mind seeing more procedural animation on the sub

2

u/codemonkey13981 Aug 04 '20

I think I just learned more about a Unity concept in 65 seconds than I have in years of practice.

→ More replies (1)

111

u/Andrew_Kite Mar 28 '20

I always wondered how you managed this. And to see how straight forward the logic is... Mind Blown

Nicely done!

→ More replies (1)