There are numerous collisions in the games. These collisions happen when the player blasts an enemy with a rail gun. They can also happen when a player bumps into a wall or collides with any object. With collisions, you can have the physics system take control of it, but other times, you just want to know a collision occurs.
So far, you’ve been firing plenty of projectiles. It would be nice to make those projectiles disappear on contact with another surface. You also want projectiles to disappear when they are no longer visible by the player. When that occurs, you’ll destroy the projectile so it’s no longer using game resources.
Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.
Collision collider types
A collision occurs when two GameObjects connect with each other. These GameObjects must have a collider attached to them. There are various types of colliders based on how configured the GameObject. Here the following:
- Static colliders: A static collider is a GameObject that has a collider added to it, but it does not have a Rigidbody added to it. Remember, a Rigidbody component indicates that the GameObject is using the Unity physics system. Thus, a static collider is a collider in not using physics.
- Dynamic colliders: A dynamic collider is when two GameObjects with Rigidbodies attached to them collide with each other.
- Kinematic colliders: These are colliders where the “Is Kinematic” option is selected. For instance, the space marine is set to this option. This option means you are electing to control manually control the position of the GameObject as opposed to responding to external forces.
- Trigger colliders: Trigger colliders are when you set the “Is Trigger” option in the Rigidbody component. A trigger doesn’t react to the collision, but it informs you when such a collision has occurred.
You’ll use all sorts of these colliders in your games.
Writing your collision code
Now to write your actual collision code for your projectile GameObject.
In the Project Browser, open the Prefabs folder and select the Projectile prefab. Click the Add Component button and select New Script at the bottom. Give it the name Projectile then click Create and Add. Don’t forget to move the script into the Scripts folder to keep things tidy.
Double-click the script to open it in the editor and add the following:
private void OnCollisionEnter(Collision collision)
{
Destroy(gameObject);
}
OnCollisionEnter()
is called during a collision event. The Collision
object contains information about the actual collision as well as the target object. For example, this will be called when a bullet hits an alien or a wall.
The Destroy()
method is what you use to delete GameObjects in code. Once you call Destroy()
, the GameObject is destroyed right after the update loop. You can also use Destroy()
on components as well.
Play your game and shoot the column. Your bullets will disappear.

There are still some bullets not being deleted. These bullets are actually passing through the mesh collider. What gives?
Collision detection settings
Believe it or not, you aren’t dealing with a bug. You are dealing with slow collision detection. The projectiles are moving too fast to be detected by Unity. This is the default behavior for Rigidbodies. Collision detection is “expensive”, meaning it requires lots of computer resources.
By setting the collision threshold to a low, Unity provides a default optimization. This optimization keeps resources minimized until you actually need them. Unity provides four levels of collision detection:
- Discrete: This is the default collision detection and designed for slow-moving collisions.
- Continuous Speculative: This is for fast-moving collisions, but not good for a high degree of accuracy.
- Continuous: This is for collisions with a high degree of accuracy. This is not for collisions that respond to moving colliders.
- Continuous Dynamic: This is also for a high degree of accuracy and collisions with moving colliders. This isn’t good for collisions that result from a rotation.
To learn more about these settings, see Unity’s documentation on the collision detection mode.
In the Project window, select the Projectile. In the Inspector, find the Rigidbody. Set the Collision Detection to Continuous Speculative.

When setting collision detection, I start with the lowest and work my up. Save and play your game. Now fire. Your bullet will disappear.
Deleting bullets without collision
There are times when your GameObject won’t collide with another GameObject. In these cases, your GameObjects will persist for the duration of the game. You could box the entire playfield with invisible colliders but there is a better way.
First, open your Projectile script in your code editor. Add the following:
private void OnBecameInvisible()
{
Destroy(gameObject);
}
Now, whenever a bullet passed out of a camera’s line of sight, the game will delete it. To test this, open the Gun script. In fireBullet()
, change the last line to the following:
bullet.GetComponent<Rigidbody>().linearVelocity = transform.parent.up * 100;
Save and switch back to Unity. Play your game and fire. Your bullet will now shoot upwards. You’ll notice that after a period of time, the bullets will be removed from the Hierarchy.
There’s a critical thing to note about OnBecameInvisible()
. This will only be called when the GameObject is invisible in ALL cameras. And yes, your Scene view is a camera. This means the method will NOT be called if you can see the GameObject in your Scene view.
Return to your Gun script and revert the last line. It should now look like the following:
bullet.GetComponent<Rigidbody>().linearVelocity = transform.parent.forward * 100;
You want your marine to shoot straight after all!
Where to go from here
Collisions are a key part of game development and games in general. Thankfully, Unity makes it pretty easy to detect collisions. Remember, if you aren’t receiving a collision event, make sure to check the colliders and Rigidbodies on both objects. You can also adjust the collision detection settings.
While collisions are great, there are times when you want fine control of your collisions. You can do this collision layers and you’ll make a few of them in the next tutorial.
Discover more from Jezner Blog
Subscribe to get the latest posts sent to your email.