GIF Parade #1

Cool girls don’t look at explosions.

Clown stripping, brought to you by our Naked Time ™ technology.

 

This is definitely not who you think it is.

How Do You Overlap a CapsuleCollider in Unity? (Dev Log #3)

The Problem

Unity physics methods like BoxCast or OverlapCapsule are incredibly useful when you need to do some custom physics calculations in your game. In my experience, you often need to use these methods in conjunction with colliders on a Rigidbody to see what they may overlap if you move them.

Unfortunately, the Physics API does not provide an easy way to take a local-space Collider and convert it into the appropriate world-space parameters expected by methods like Physics.OverlapCapsule.

For example, here is a simple MonoBehaviour that tries to check each frame if its CapsuleCollider is overlapping anything in the scene:

The Solution

It is possible to calculate these world-space parameters yourself manually of course, but it is tedious and error prone because the transform of the Collider or its parent can change rotation, scale, or position arbitrarily.

In the course of developing Just In Time Incorporated I got annoyed enough with this problem to write an extensions class that contains the world-space conversions for BoxCollider, SphereCollider, and CapsuleCollider. The class also contains wrapper methods that take a Collider and call BoxCast/SphereCast/etc for you.

The class from above would now look like:

You are also free to use the ToWorldSpaceCapsule/Box/Sphere extension methods yourself of course:

 

Get it here on Github (MIT Licensed):

https://github.com/justonia/UnityExtensions/blob/master/PhysicsExtensions.cs

 

Bouncing a Ragdoll is Easy Right? (Dev Log #2)

A character ragdoll in Just In Time Incorporated bouncing off a trampoline

Let the Prototyping Begin

We are currently hard at work prototyping another batch of levels for Just In Time Incorporated. One idea we’ve had in the back of our minds for a while now is a scenario where the player needs to save people jumping out of a fiery building. As an idea, this level inhabits that sweet spot where the entire concept hits every design pillar for a game in the action-puzzler-bullet-time-insurance-agent genre.

After a few hours in Unity and many ProBuilder primitives later, the level was roughed out:

A prototype version of the building jumper level in Just In Time Incorporated

Imagination Meets Reality

The goal for the player is simple — have them place a bouncy pad under the falling person and voila they bounce off a few times and are saved! All that is needed is to put a really bouncy material on the physics colliders of the pad and things will work… right?

A ragdoll character in Just In Time Incorporated falling onto a bouncy pad and not moving much.

Yawn. In slow motion the bounce is even more boring if you can believe it.

Reality Disappoints

Ironically one cause for the underwhelming result is what makes ragdolls so fun in the first place — their floppiness!

Each of our characters has a physically simulated ragdoll composed of 16 rigid bodies and all their connected joints. In a normal game it would be overkill to have such a detailed ragdoll. In Just In Time Incorporated the player constantly interacts with characters in slow motion and the extra fidelity really cranks up the fun factor. Unfortunately when the ragdoll hits the bouncy pad the flexible joints bend and end up absorbing much of the force that would otherwise cause a more respectable bounce.

A character ragdoll rig in Just In Time Incorporated

 

Reality Meets Physics Hacks

The other root issue with the boring bounce is that physically speaking the bounce pad is an impossibly solid object without any ability to compress and store energy (thank you rigid body physics). One potential hack might be to scale the colliders on the bouncy pad down and then back up to simulate the compression of a soft body. If experience is any guide this is a one way ticket to buggy-physics land with all its interpenetrating object fun!

Thus it was time to whip out the default tool of any Unity physics hacker: AddForce!

Code snippet showing some Unity physics hacks to simulate bouncing

It took more than a few hours to arrive at something that looked good and worked well in slow motion. All of the following hacks had to be layered together into a final Frankensteinian coroutine:

  • Magic number your way to a force value that feels good at an arbitrary collision velocity reference point. That poor jumper character had to die at least 100 times to find out that at -10 m/s a force value of 24 felt right. R.I.P.
  • Scale the force applied if we are in slow motion.
  • Scale the force applied to diminish over an arbitrary time (0.4s felt good).
  • Scale the force based on the collision velocity projected onto the local up vector of the pad. Without the projection, a sideways moving character will have a seemingly large velocity magnitude even if they are barely falling onto the bouncy pad.
  • So the player can somewhat control the direction of the bounce, calculate a direction from the collision point to the center of the pad and then average it into the collision normal.
  • In addition to adding force to the body part that hit the pad, add an impulse to its parent body part as well (e.g. foot hits, add force on leg too). Using an impulse adds a nice “oomph” to the bounce response.

It’s Fun!

With all the time spent trying to hack together a satisfying bounce, there was always the worry that the level itself might just not be fun. Luckily placing pads to make people bounce and ragdoll through the air is very, very fun. Phew.

An action shot of multiple characters falling through the air and bouncing off of pads on the ground.