Tutorial – Unity Basics: Physics

Like the others, this post is basically a collection of notes taken from the training videos on Unity’s website.



-Allow the game object they are attached to to interact with other colliders, provided one has a Rigid Body component attached.

– A Rigid Body provides Mass and Gravity

– Shapes can be spherical, box or capsule, a combination of these in hierarchy, or a more complex mesh.  You can use a mesh collider, matching the mesh of the object its attached to, but this may be too complex and affect performance.

-Colliders have a Green outline in scene view.

– When a collision between objects occurs, an event called OnCollisionEnter occurs. This can then have scripting applied to do something when objects collide.

-There is also OnCollisionStay which is active during a collision, and OnCollisionExit which activates once objects are no longer colliding.


-Check the ‘Is Trigger’ checkbox in the objects settings panel in the inspector tab. Now, when a collision occurs, rigid bodies wont bounce off it, but instead pass through. This way they can be used in code.

-Standard practice is to make trigger objects static, and then pass rigid bodies thorugh them.

– Example code phrases: OnTriggerEnter when object passes through trigger collider, OnTriggerStay whilst an object is inside or in contact with a collider, OnTriggerExit when the object leaves the trigger zone.


– Rigid Bodies are components that allow game objects to be affected by physics, and have properties such as mass, gravity and velocity.

– Add rigid body components using the ‘Add Component’ button in the Insector window, or along the top menu bar.

– As a general rule, any object that will move in your game should have a rigid body component applied.

– In RigidBody component options you can freeze position rotation and scale on the 3 axes. So for example to make a 2d platform game freeze the corresponding axes.

– Any rigid body object must have a Collider attached. An object without a rigid body will simply float in mid air.

There are several properties in the rigid body inspector panel:

Mass: How it will react when colliding with other rigid bodies. For example, a high mass object will not react as much as a low mass object when they collide.

Drag: How fast or slow an object will lose velocity when other forces stop acting on it.

Angular Drag is how fast or how slow an object will stop rotating when other forces stop acting on it.

Use Gravity Allows you to toggle whether an object will be affected by the game’s global gravity settings, which can be changed in Edit> Project Settings> Physics.

Is Kinematic affects whether or not a rigid body will react to physics. If checked, a floating platform will stay in place when another rigid body lands on it. If UNchecked, the platform will move when an objecct lands on it.

Case Study: Moving Platforms. When a static object (one without a rigid body) such as a platform moves, unity will check all static objects positions for accuracy. This can eat a lot of memory. Therefore, making a platform a rigid body  with ‘Is Kinematic’ switched on is much more memory efficient than making a static object move.

Interpolate and Extrapolate smooths jittering that can occur when an object moves. Interpolate works by looking at the previous frames movement, while Extrapolate predicts based on the next frames movement.

– Collision Detection determines the type of collision detection used, and generally discrete is fine. Continuous can be used when fast moving objects collide with static objects, and Continuous Dynamic can be used when fast moving objects are going to collide with each other.

Constraints affect the way an object can move. For example, in a 2d platform game, objects should only move in the x and y axis, and in a tetris style game, blocks should not rotate when they collide with blocks underneath.


Is a scripting function used to apply a velocity force to an object with a rigid body component attached.  Look at the following Code:

void OnMouseDown()


rigidbody.AddForce(-transform.forward * 500);

rigidbody.UseGravity = true


With this code, when the left mouse button is clicked on the object, a force of 500 newtons is applied, and gravity affects the object. The transform.forward command is a shortcut for the objects z direction, and the – sign indicates that it will go the opposite way.

The second line of code regarding gravity is optional, but the first is required. There is an optional Function called ForceMode that can be applied in the first lines brackets, to affect how the physics will behave.


Is a scripting function used to apply a rotational force to an object with a rigid body component attached.  Look at the following Code:

 void FixedUpdate ()
        float h = Input.GetAxis("Horizontal") * amount * Time.deltaTime;
        float v = Input.GetAxis("Vertical") * amount * Time.deltaTime;

        rigidbody.AddTorque(transform.up * h);
        rigidbody.AddTorque(transform.right * v);

When the left key is held, the object will rotate around its vertical axis, and inertia will keep it spinning when the key is released (affected by the Drag property in the objects rigid body settings). When the Up key is held, it will spin around it’s horizontal axis.


– Are materials that instead of affecting an objects appearance, affect how itreacts in the physics engine. For example- a basketball will bounce off the ground differently to a bowling ball, and this can be reflected by a Physic material.

– Physic Materials can be created in the create button on the project panel then edited in the inspector panel. The bounciness and friction can be assigned values between 0 and 1.

– Physic Materials are not assigned components of their own. Instead, they are applied to the Material slot of the Collider component.


– Joints are components on objects that can affect or restrict movement.

– Fixed joints work like a parent in a hierarchy, and lock a game object in place to other objects or rigidbodies.  Note that force and torque settings can be applied to allow the joint to break and release the object.

– Spring Joints are like sections of chain, and used to create things like wrecking balls etc.

– Hinge Joints are useful for things like doors.  The hinge can be placed at a point on an object and rotated about a certain axis.  They can also be given motor settings to keep them spinning of their own acocrd- like revolving doors for example.  You can set restraints on how far the joint will rotate, and break forces to break the joint.


– Casting a ray from a source point, and determining if any colliders were hit by the ray. For example, shooting a bullet from a gun.

– A raycasting scripting function looks like this:

Physics.Raycast(Vector3 origin, Vector3 direction, RaycastHit hitInfo, float distance, int LayerMask);

– a Vector3 is an (X,Y,Z) position stored as a variable. In this case, the origin would be the barrel of the gun. We also need the direction info in a second Vector3 to show the bullets trajectory.

– Unity has a type of Variable called a Ray which can hold two sets of Vector3 co-ordinates.  We could store these in a variable like so:

Ray myRay = new Ray(Vector3 origin, Vector3 direction);

where myRay is the name of the new variable, it is of type Ray, and contains origin and direction data.

We then replace our original function code with:

Physics.Raycast(myRay, RaycastHit hitInfo, float distance, int LayerMask);

RaycastHit stores information on the colliders hit by the ray, so that it can be queried in code.

float distance is an optional variable to determine how far the ray reaches. It defaults to infinite.

For example a parachute can be set to open when it reaches a certain height above the ground. Corresponding drag and object animations can be scripted when the ray hits the environment below.

int LayerMask is the number of a particular object layer in which you can place objects if you wish the ray to ignore them.

Debug.DrawRay function can be used to visually see where a ray occurs.


– Collision in unity require at least one of the objects in the collision to have a script attached with an OnCollisionEnter command attached.

-The code below shows how a ball hitting boxes with object name ‘prop_powerCube’ can be made to destroy the boxes when a collision is detected.

public class DestroyCubes : MonoBehaviour


void OnCollisionEnter (Collision col)


if(col.gameObject.name == “prop_powerCube”)






the variable col holds data on the instance of the object the ball collides with, and the col.gameObject.name is just one of the fields of information it contains.


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s