5SD064

Unity Collision detection and the Collision Layer Technique that works best

Today, I am going to be writing about Unity’s difficulties with collision detection and how to solve it using a layered system.

Collision detection plays a vital role in game production whether it’s the collision detection between players and enemies or even players and the game’s boundaries. This is because without it the fundamental aesthetics and mechanics could never be met. I mean who would want to play a game where your character can’t hurt enemies and can fall off of the game world? I would not enjoy playing it as it would not be a very aesthetically exciting game to play.

In Unity, collision detections can only occur once the programmer has placed a physics collision object on whatever game objects they wish to have collisions. In my game I used collision boxes instead of meshes as the game is viewed from a 2D percpective. Meshes could have been used but since it can take a while to implement, it was not worth the time it needed.

Once a collision box is implemented it automatically deletes itself and any other game object it had collided with. Unity has this implementation as a set characteristic within it’s framework and in order to change these characteristics to suit my needs, collision on trigger exit or on trigger enter script had to be written.


void OnTriggerEnter (Collider other){
   if  (other.tag == "PlayerBullet"){
   /*this is where you add the collision characteristics you want.*/
      Return; //this return is only used when you don't want a collision to occur.
   }
}

However, this way of implementing collision characteristics can be a bit tricky to get working. For example, while using this design I spent countless hours trying to prohibit enemies from deleting or colliding with each other. In Unity there is an easy way to fix this by using the collision layer technique.

In most games layers are implemented in order to distinguish between background, game field and UI assets. The collision layer technique is similar in this aspect as all game objects get assigned a specific layer from which it can be referenced.

Once the game objects have their collision layers, in order to set the characteristics you have to go to Edit -> Project Settings -> Physics or Physics2D depending on your implentation. Once there a collision triangle shows up where you see a bunch of tick boxes, then all you need to do its untick a box where you don't want a collision detection to occur. Of course on trigger functions are still required for characteristics like damage taken.

This new design method was chosen due to an older student's suggestion and research I had already conducted on how to define my collisions in a better way. However, the reasons for choosing this design in the long run are because of collision layers being of a more agile method for doing detections. This method is defined as agile as it can be edited or changed quickly and efficiently within the program while allowing room for more quick changes in the future. While working in agile SCRUM, the implications of the collision layer technique are more efficient for this design.