Using the built-in physics engine to make reactive behaviours and environments. You must have some basic experience of Unity and Scripting.
In game engines, there is no assumption that an object should be affected by physics — firstly because it requires a lot of processing power, and secondly because it simply doesn't make sense. For example, in a 3D driving game, it makes sense for the cars to be under the influence of the physics engine, but not the track or surrounding objects, such as trees, walls, and so on — they simply don't need to be. For this reason, when making games, a Rigid Body component is given to any object you want under the control of the physics engine.
Physics engines for games use the Rigid Body dynamics system of creating realistic motion. This simply means that instead of objects being static in the 3D world, they can have the following properties:
- Mass
- Gravity
- Velocity
- Friction
While more crucial in game engines than in 3D animation, collision detection is the way we analyze our 3D world for inter-object collisions. By giving an object a Collider component, we are effectively placing an invisible net around it. This net mimics its shape and is in charge of reporting any collisions with other colliders, making the game engine respond accordingly. For example, in a ten-pin bowling game, a simple spherical collider will surround the ball, while the pins themselves will have either a simple capsule collider, or for a more realistic collision, employ a Mesh collider. On impact, the colliders of any affected objects will report to the physics engine, which will dictate their reaction, based on the direction of impact, speed, and other factors.
- Open
Assets/Scenes/BaseScene
. - Make a cube, add a
Rigidbody
component, press play. Watch it fall forever. This is the gravity simulation. - Add a Floor Prototype prefab. Make the coordinates (Position) x: 0 y: 0 z: 0.
- Press play.
Also, if you change the Physic Material
on the Box Collider
, this will change how the box interacts with other surfaces.
- Select a cube, select the bouncy material.
- Press play. They should bounce.
- Open
1_Physics - Basics
scene - Import funny shaped models
- Add Colliders, Rigidbodies, check settings
- Making composite shapes from Colliders for efficient collision checking
- Add Physic Materials to make stuff bounce
- Play around with mass and drag settings to understand the effects.
- Open
2_Physics - Triggers
scene - Make a transparent box, open the template script
PlaySoundOnTrigger
- You will have to fill in how it works. What it should do is play a sound whenever the player enters the collider volume.
- For bonus points, how can you ensure that the collider will be a trigger?
- Open the hover pad script and make it add a force if the player is staying in the trigger volume.
- If something gets hit, make it destroy it self and make an explosition using the particle systems in the standard assets.
- For the brave, make a door that makes a noise when it shuts.
- Best Practices
- Make a Pinball machine tutorial (2d)
- Interesting things with math and physics e.g. Verlet Softbody, Rigidbody, and Chain
- Boids and Cloth
- MouseDown, clicking things in the screen space
- Raycasting, shooting lasers to hit things in space
- HitEvent, moving the process of object effects on the objects themselves
Unity gives you a pile of events on MonoBehaviour that work with mouse input
void OnMouseUpAsButton()
{
// Filters on and off mouse to give button effect
}
private void OnMouseUp()
{
// Check main mouse button release
}
private void OnMouseOver()
{
// Maybe change colour
}
private void OnMouseExit()
{
// Leaving the boundary of an object collider
}
private void OnMouseEnter()
{
// Entering collider of that object with mouses cursor
}
private void OnMouseDrag()
{
// Simple way to drag physical object with a cursor.
}
private void OnMouseDown()
{
// Get first down event from mouse button one
}
- Open
3_Interaction - Mouse
scene - Fill in the blanks on the
MouseDown.cs
script to make the head fly off when you click on them. - Add the
DragRigidbody.cs
script to each head and see if you can get them to slide around. - Experiment with different force types in the
AddForce
call.
- Open
4_Interaction - Raycasting
scene - Fill in the blanks on the
Raycasting.cs
script to make the head fly off when you click on them. - Open the
HitEvent.cs
script and transfer the same idea from head to the boxes. - Experiment with
- different force types in the
AddForce
call. - rigidbody mass
- rigidbody drag
- different force types in the
- Add a Cross hair UI to the scene so you can see the centre point.
-
Klak - creative coding kit for Unity that you can prototype things with then steal the code