Let’s look at Physics and Unity — how to make them work!
So you are probably wondering about how we are going to make things work in your video game in terms of Physics. You know, eventually you are going to have things colliding all over the place. Good thing Unity comes with it’s own built in physics system to make things easy!
So when you are creating objects you might notice in the Inspector that there are things called colliders already built into your object. For example when you create a new 3D Cube it will have a Box Collider. When you create a new 3D Sphere it will have a Circle Collider. Colliders are what tell Unity when things are colliding.
There are 2 types of collisions that you will be using. Hard Collisions and Trigger Collisions.
Hard — This is when two objects collide and bounce off each other. Such as a bat hitting a ball.
Trigger — This is when 2 objects collide but “absorb” and not bounce. For example when Mario ran into a coin he collects it, the coin disappears and it doesn’t effect Mario’s movement.
The way to choose is something called Is Trigger located in the Collider on the object. If this box is checked then the object will pass through the colliding object.
No you know about colliders you will need to know how to make them active. To do this, you need a Rigidbody which will add physic properties to an object. Some of these properties are things like Gravity, Mass, Drag and Kinematic.
To add physics to the object you will add the Rigidbody by clicking on the Add Component in the Inspector and choose Rigidbody (if you are building a 2D game you will add the Rigidbody2D).
You do not need to add the Rigidbody to both objects for them to work, but choose wisely as if there are a lot of objects in the scene this can be a little performance intensive.
Important note on adding a Rigidbody — there is a Use Gravity button and it is checked by default. This is going to activate the gravity and you will notice things falling through platforms or off the screen. Ensure you have this button unchecked if this is happening.
That’s it for now — tomorrow we will continue with this topic and delve deeper into the code behind collisions with OnCollisionEnter vs OnTriggerEnter.