Check out what I made! (Make-it and Break-it)

I had the best time making this VR experience. The player stands on a ledge, uses her hands to draw a creation, and then tosses it over the edge to watch it hit the floor and shatter. This experience was fun to make, and even more fun to play test. Breaking things is fun!

The player’s creation is made of little adjoining spheres. To give the player the ability to create, I made a sphere prefab (i.e. template). I then wrote a script to instantiate the prefab at the location of the player’s controller while the trigger button is held down.

To prevent the spheres from falling, I added a Rigidbody, but set the “Is Kinematic” (i.e. “ignores physics”) value to True.

I wanted to give the player the ability to interact (grab, drop, etc.) with the entire creation (vs. an individual sphere), so I parented the spheres under an empty game object. The player cannot see this parent object — it is used simply for reference. I set the “Is Kinematic” value to True on this object as well.

To allow the player to grab the parent object, I wrote a script that locks the transform (position and rotation) of the parent object to the player’s controller while the grip button is pressed. As a result, when the player moves her controller, the object moves with it. I also added logic to perform this lock only if the player is actually touching the object.

To drop the object, I added logic to unlock the parent object from the player’s hand when the controller grip is released, and set its “Is Kinematic” (i.e. “ignores physics”) value to False. This causes the parent object (and attached sphere child objects) to fall straight down. I wanted the player to toss the object, however, so I also applied the velocity and angular velocity from the player’s controller to the parent object the moment it’s released.

Unfortunately, this only partially worked. Since the angular velocity was applied to the parent, and the parent is at a location dissociated with the children, the creation seemed to spin out of control. Therefore, I wrote some logic to instead apply the angular velocity at the point the controller is touching the creation. This took a little math. I found the offset between the controller and the parent object. I then moved the parent object to the position of the controller, and moved the child objects by the offset. By doing so, the creation doesn’t appear to move, but the angular velocity is applied at the appropriate location.

At this point, the player was able to draw and toss her creation, but the creation didn’t collide with the floor — it fell straight through. To give the spheres bounce, I tagged the sphere prefab, and created a script that holds all objects with that tag in an array. The script loops through the array and updates the “Is Kinematic” value to False for each sphere. To ensure the spheres keep moving in the direction tossed, the script also applies the angular/velocity from the parent.

And all that almost worked. But, the moment the updates were applied, the spheres collided and bounced off one another, and the creation fell apart. To prevent this early collision, I reduced the collider inside the sphere prefab to half its diameter, and added logic to only add new spheres when the colliders are at least a separation apart. This had the additional benefit of making the spheres appear more evenly spaced.

Since the colliders no longer touch, the spheres don’t collide with each other.

But there was still a problem. As the creation was released from the player’s hand, the spheres closest to that hand collided with the hand and broke off on their own trajectories. To prevent that collision, I added a spherecast to the controller, and added logic to wait until the spheres are outside of it(i.e. out of reach) before applying the physics/changes.

Tah-dah! Now the player could draw a creation, grab it, toss it, and watch it shatter as it hit the floor! After adding a little logic to destroy all objects when the “A” controller button is pressed (to start over), my proof of concept was complete

I learned a ton about the physics engine during this project, and it was really cool to see my idea come to life. I must admit, however… although this solution does work, the performance isn’t great (especially when applying the “Is Kinematic” update for a large number of objects). So, I’m still on the lookout for a better solution. In the meantime, I thought it would be fun to share what I have so far. I hope you enjoy watching as much as I enjoyed building this!