I created a player controller script to let me control the individual bodies using my keyboard. I wanted to be able to set which keys to use to control a player within the inspector, so I included a public string variable for each respective direction (up, down, left, right), and parsed the string as a Keycode value using this code:

(KeyCode)Enum.Parse(typeof(KeyCode), string)

The script let me control one body using the arrow keys and another using the WASD keys.

With that implementation, I began testing the amount of force needed in order to separate the bodies once they had already merged.

Moving bodies in opposite directions

Moving a single body only

Mindful that it was now time for me to move on, I decided to clean up my code, which had gotten pretty messy with all the testing. I created a boolean to switch between my different scaling algorithms during play mode, and used NaughtyAttributes to show/hide inspector variables specific to a particular scaling algorithm based on that boolean.

I want to create a pulse effect on the scale of a body using a sin wave function. Different bodies should be able to pulsate independently of one another, so I added a random offset value within each player constructor that generates on runtime. It’s not quite there yet…

One significant issue with my implementation thus far is that the pulsations only occur when one body is within the attraction radius of another. Another obvious issue is that the pulse amount scales the object negatively if made too large. This is because I haven’t yet implemented logic to pulse relative to scale.

Surprisingly, the merging of objects seems to work even when the objects are pulsating at different offsets. How convenient!


Tags: gamedev unity csharp physics vfx scripting input