Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Collision or Trigger?- Game Dev Series 09

S.J. Jason Liu
Nerd For Tech
Published in
4 min readMay 4, 2021

--

Should I make a trigger? Or a collision? That is a big question.

Previous: A Little Physics in Unity

We created an enemy and made it moved. However, it stopped above our player which is not the result we want. To deal with this, we need to understand the 2 main collider function-
OnCollisionEnter & OnTriggerEnter.

Before we bump in with collision, we need to do some adjustment with our enemy. First we need to decrease the falling speed from gravity to an adjustable variable.
Let’s uncheck the gravity of enemy, then we create a C# script called Enemy and attach to it.
Within the script, we need to create a falling movement which is just like our laser movement with opposite direction.

Create a variable for speed, and make our enemy move with real world time as usual.

[SerializeField]
private float _speed = 4f;
void Update()
{
transform.Translate(Vector3.down * _speed * Time.deltaTime);
}

With a downward movement is not enough, we need to make it move back to the top if it reached the bottom. And also make a random movement on X axis,

[SerializeField]
private float _speed = 4f;
void Update()
{
transform.Translate(Vector3.down * _speed * Time.deltaTime);
if(transform.position.y <= -5.5f)
{
float randomX = Random.Range(-9.5f, 9.5f);
transform.position = new Vector3(randomX, 7.5f, 0);
}
}

We create a float for randomize the x axis and set it as a new start position if the enemy is go below -5.5 on the y axis.

Now our enemy should become unpredictable.
Let’s examine the collision with laser and player.

All of our game objects have collider, therefore it should be a clear result for collision.

As you see, everything goes wild. That’s because we used the wrong collision type in our game.
There are 2 type of collision:

  • Physics collision
  • Trigger collision

Physics collision, as OnCollisionEnter we used in the script commonly, is the result shown above, everything bumped into each other. On the other hand, trigger collision is more like a concept collision. You are not actually hit something physically but a switch or remote as it shows.

OnCollisionEnter mostly used for a real objects such as car crash or melee punch. And we usually use trigger collision as OnTriggerEnter in a script at some non-realistic objects such as power-up orb or floating soul pieces in a game. Which we are going to use the same collision to our weapon and damage collision.

To change our collision to trigger collision, simply check the Is Trigger in the collider component.

Don’t forget to check all the trigger from Player, Laser, and Enemy.

Then we can play again to see the result.

And that’s more like a laser works in real life(?). The next step is to make our laser eliminate the enemy.

As the description above, we use OnCollisionEnter & OnTriggerEnter to deal with the collision. In our situation, we use OnTriggerEnter. Let’s add the code into our Enemy script.

Below the Update(), typing OnTriggerEnter and it should help you to create a method,

private void OnTriggerEnter(Collision other)
{
}

When we called this method, it will automatically create a variable for it to recognize the target of collision called “other” which we will use to identify the target whether is Player or Laser.
To do this, we will use another function in Unity called “tag”.

“Tag” is used to separate the type of game objects in Unity. For example, if you set the script function can only works on the game object with tag “Player”, it would not works to your environment game object you give a tag “Environment”.

We could create a tag called “Laser” for our Laser prefab, and “Player” for our player.

We can add an if-statement to OnTriggerEnter then destroy the laser and the enemy after collision happened,

private void OnTriggerEnter(Collision other)
{
if(other.transform.tag == "Player"
{
//damage the Player
Destroy(this.gameObject);
}
else if(other.transform.tag == "Laser"
{
Destroy(other.gameObject);
Deatroy(this.gameObject);
}
}

So far we could not destroy the Player in our script, we can still destroy the enemy when a laser collide to it.
And this is how it looks like,

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

S.J. Jason Liu
S.J. Jason Liu

Written by S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

No responses yet