Having interactive foliage in your game helps bring some life to your environment. It makes everything feel more alive. There are lots of different ways to handle interactive foliage. The easiest way is to stick a trigger collider on your GameObject. When the player hits the trigger just move it back and forth on the x-axis. In this post we will go over how to kick that up a notch.
If you read the Modeling 2D Water with Springs posts, you will know that I love springs. For that reason we are going to model our grass with a spring. The first approach I took when making interactive foliage was to handle all the animation in the vertex shader. With Unity that will break batching so I had to fall back to the method detailed in this post.
The first thing we need is a trigger collider. You can use a BoxCollider2D or a CircleCollider2D. When the player enters the collider we take note of the _enterOffset which is just the distance between the player collider and the foliage collider. We need the _enterOffset so that we don’t start bending the foliage until the player has passed the midpoint of the foliage. Things don’t start bending until you are dragging them past their resting position. That’s just how the world works.
1 2 3 4 5 6 7 |
|
We will use OnTriggerStay2D to keep track of the player + foliage interaction. Once the player has moved past the midpoint (offset and _enterOffset will have opposite signs) we set some flags and start to bend the foliage. Bending the foliage is done by just sliding the top 2 verts of the foliage quad back and forth. At this point, the foliage bend is entirely based on the position of the player.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
Once the player exits the trigger the spring takes over and it will handle simulating the foliage oscillation (springs are the best!). We apply a force to the spring and let it do its thing. Here the _isRebounding flag is set which lets the spring know to take over (that is all handled in the Update method). When the spring acceleration dies down the oscillation is stopped. This is done as an optimization. There is no reason to continously update the mesh vertices for movements too small to see. You can the result in the video below the code block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
What we have now is a firm base: interactive foliage and a configurable spring system to give it some life. Adding something like a wind force is super easy. All we have to do is use a sin wave to vary the wind and apply the force to the spring.
1 2 3 4 5 6 7 8 9 10 |
|
The last piece of extra polish we can add is to make our foliage part when we jump. This is also a simple addition due to the solid base we have already set. We just need to detect when the player jumps into the foliage and apply a force to the spring. We use the positions of the player and foliage to see if we should apply the force to the left or right. Nice and simple.
1 2 3 4 5 6 7 8 9 10 |
|