Objects Follow Player in Formation

May 16, 2019

Follow Player Script for Unity

Follow with Formation and Noise

Vimeo – Demonstration of the FollowFormation Scripts with Noise Features

By: Me

This clip just shows off the different aspects and functionalities for my FollowFormation scripts so far.

I was working on a system in Unity for a class project that would allow a player to collect objects around the map by running into them, and then they would follow the player around in some sort of formation. This ended up working better by splitting the concept up into two classes: Leader (on the player object) and Follow (on the following objects).

Leader script

Since we wanted to control the general formation of these objects as well, we’ll start with the Leader script. To make a simple and easily modifiable system, we decided on making a system based on a bunch of transforms childed to the player that would serve as the formation locations. The Leader script basically just grabs all of the children objects and holds them in an array. This is the formationPositions array. It then has a public static method that gives one of these gameObjects and then increments a counter so the next time it is called it returns the next one. This leads into the Follow script.

Follow Script

The Follow script is placed on the objects that will follow the player when collected. The first action is having them be collected in the first place. This is done with a simple collision that starts the following (and turns off the collider so they don’t interact with anything, including the player again). To help with the overall idea of being in formation, the Follow object actually grabs that next gameObject in the formationPositions array to follow around. This is how each object actually gets a different spot in the formation to follow around. With this setup, we just needed to determine how we wanted them to follow.

Various Follow Methods

First, I tried using a combination of LookAt and adding a velocity in the transform.forward direction. This was solid for giving them a bit of an organic movement feel, but it was not very controlled. If the speed was very different from that of the player, their movements felt awkward and they would also tend to clump up. This could be an interesting solution route, but it would take a lot more work to function properly.

Next, we looked at simply setting the transform.position to the follow object. This wasn’t great as it looked very mechanical, especially immediately upon collecting the object. It would just teleport behind the player.

Finally, we settled on using a Lerp function along with the transform.position. With use of an additional speed variable, this gave us a nice, smooth motion that was easy to modify to give various feels.

FormationNoise

After settling on the foundational movement for the following objects, we wanted to look into adding some noise to their movements to make them feel a bit more lifelike. Since I knew there were going to be a few different options of noise I’d like to switch between to see which ones felt better/worse with different parameters, I looked into setting up an enumerator with a switch case statement to easily switch between these different options. I simply created an enum called Motion that held a value for each of the different types of noise I made, and had a switch case check the Motion motion to determine which noise that specific object would use. This ended up being extra nice as I could also have different objects testing different types all at once.

It should also be noted that in order to keep things simple, I added the FormationNoise script directly to the formation position gameObjects that were childed to the player. Moving these simple transform objects would in turn move the following object following it as it is specifically given this gameObject transform to follow.

Finally, since everything was dealing with keeping these objects in the general area designated for their spot in a formation, the initialLocation is recorded on Start so we know its initial localPosition. This helped move the objects around this point, as opposed to continually adding or something and having it fly out of position.

Noise Methods

The first noise we tried was a simple random range applied to the entire position vector of this formation position object. I created a variable for the max and min of the random range to randomly sift through, and every frame a value in this range would be added to the initialLocation. This actually ended up being pretty nice for creating a “buzzing” or “vibrating” effect, which was nice since these would eventually be applied to bee characters for the ‘ project, but other than that it was not very interesting motion.

Since that method seemed a bit too hectic, I wanted to test just adding some noise to a single dimension. I chose the x-axis to randomize along since that seemed like it would give the most interesting results from my camera top view. This did seem a bit more controlled, but still mostly just had a “buzzing” feel to it.

Finally, I thought about what general motion I would like along with the noise and I thought a nice overall controlled swaying and/or bobbing would be nice to have with the noise. This method got a bit more involved. I created a sway variable, which would determine the amplitude they would drift on a given axis (for initial testing, I just did the x-axis). I also created a swaySpeed to determine just how fast they moved between the max and min sway locations.

Since I liked the “buzzing” feel of random moving the point around a small range, I kept that for the y-axis and z-axis. I just had the sway controlling the x-axis movement. This had the object buzz around a bit as it moved back and forth relative to its given initial formation position. This all led to a nice movement that was still very controlled, but felt a lot more alive.

POTENTIAL IMPROVEMENTS

The movement of the objects when they are following a significantly fast player still looks pretty direct and parented. It appears that if their overall speed is much greater than the movements/speed of their internal noise methods, the noise gets a bit lost or drowned out. This is something I will just have to player with more when dealing with the relative speed of the following objects and the player.

Possibly control more axes of noise a bit more. Adding a bobbing effect by swaying in the y-axis would give a nice effect. If we control too many axes, we’ll have to recheck if it’s still a nice effect or if we’ll need to add more actual noise to the movements again to keep them from feeling too robotic.

Currently the setup of the Leader’s formationPositions array only makes an array the size of how many children the player object has at startup. This makes the system a bit rigid, or require having a bunch of unneccessary children objects for a while with possible errors for large numbers of followers exceeding the array count. A more variable system might use a list instead of an array, or just a large array with some method that creates gameObjects as followers are gained to add to that array.