Rigid Body Physics

In Unity 5

Rigidbodies are Unity’s snap in physics simulator for any and all game objects.  Rigidbodies interact with other game objects and twist and turn as they hit colliders in either 3D or 2D space.  Your choice.  In fact, with regular rigidbodies, you can choose what forms of position and rotation change are off limits, but I’ll get to that down below.  For now, create a game object and add a rigidbody from the physics component section.  For a 2D game, select a rigidbody2D from the physics2D section.

Screenshot_1

First, I’m going to talk about the standard rigidbody and what it can do.  If you’re more interested in using rigidbodies for a 2D game, you can skip below, but I should note that you can still use the standard and it comes with some added features the 2D version doesn’t have.

First off, a rigidbody has mass.  This influences largely two things.

  • First, it serves as a pecking order when objects with rigidbodies collide with their bounding boxes and such.  The smaller one gets pushed back … relative to incoming velocity, of course.  Basic physics.  If two objects are going at the same speed, the lighter one gets pushed back.  If two objects are traveling at different speeds, the lighter one might win if it’s going much faster.
  • Secondly, mass determines how much force you need to apply to the object to get it to move.  This is done through an attached script using the AddForce() method.  Naturally, a rigidbody with a large mass needs a bigger number passed through the method in order to move the same speed. For this reason, I usually try to place all of my game objects around the same mass.  This doesn’t ordinarily affect my games, because most enemies and allies are the same size.  However, mass becomes a great tool to vary when you have asteroids or pillars that you don’t want the player to move so easily.

Drag and angular drag aren’t largely important.  They affect how quickly your object slows down.  If you want a faster stop, try setting these higher.  However, if you are experiencing a lack of acceleration because of this, I suggest using the ForceMode.Impulse or ForceMode.VelocityChange when scripting the the AddForce() method.

Lastly, you may constrain position and rotation on any axis, making rigidbody resist reacting no matter how massive the colliding object might be.  This is extremely useful if you happen to be building Pong or have an object you want to move around but that cannot be affected by other objects.

Screenshot_3

The screen below is a capture of the 2D rigidbody.  Similar, yes, but with some added features.  We can leave all of these to their defaults in most cases, though particularly useful are the Fixed Angle check box and Gravity Scale box.

Fixed Angle checked means that when the 2D object collides, it will retain it’s rotation instead of turning and spinning in reaction to having just hit something.  In my opinion, this should almost always be checked in a 2D game.

Gravity scale adds another feature to a 2D game.  Gravity will affect how fast the object falls down toward the bottom of the screen.  Gravity can vary between sprites or game objects in general that are using the rigidbody2D.

A note…

Gravity still exists in a 3D project and is applied to every normal rigidbody automatically.  However, that setting is modified outside of the object’s inspector.  To change the gravity of a 3D game, go to Edit > Project Settings > Physics.  There, you may set the game’s gravity to any direction you wish.  It can be up, down, left, right or diagonal, but the setting is constant across all scenes, so choose well.

Screenshot_4

That’s all I have for today.  I hope you enjoyed this tutorial, so come join me for another!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s