Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction to Collisions and Colliders
2.
How to add a Collider Component in Unity?
3.
What are the different types of non-primitive colliders?
4.
Frequently Asked Questions
4.1.
How many types of colliders are there?
4.2.
What is the difference between collisions and colliders?
4.3.
Do we need rigid bodies for implementing collisions?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Collisions and Colliders

Introduction to Collisions and Colliders

In layman’s terms, Collisions are referred to instances in the real world where two objects collide and physically touch each other. However, in Unity 3D, which is a popular gaming engine, collisions are defined using Collider2D. Collisions and colliders are inter-related terms used in the development of games and animations.

Collider2D is a component which defines the region where gameObjects interact with each other during a collision. Therefore, colliders are the regions where gameObjects are solidified and can be interpreted as solids interacting and colliding with each other. Regions are considered instead of definitive shapes because not all gameObjects are shaped exactly as they are defined. For example, in the case of definite shapes like squares, triangles, etc., we can specifically define boundaries covering exact edges and manipulate the region area around them. This can be visualized in the following figures of triangles and squares.

Source: Link

However, in the case of shapes with indefinite boundaries like the gear shape, we consider approximate shapes and therefore, an approximated regional area is taken into account for these gameObjects. The green boundaries define the collision region for each shape in the figure. For normal shapes, the green boundaries are definite and well-defined but note how the collider doesn’t wrap around the gear’s teeth perfectly, but instead, Unity made approximations on the collision regions of the shape, leading to a simpler collision region.

Collider2D is a defined component in Unity that detects collision or intersection of gameObjects in a defined 2-D coordinate space and tells when the objects are touching or even overlapping. 

If the gameObject sprite is a complex drawing, just an approximation of its shape will be necessary to work because the Collider 2D is invisible and not distinguishable during the gameplay.

Trigger 2D is a particular behaviour of a Collider2D which is used when we want to detect when one collider overlaps another one without creating a collision.

RigidBody 2D is a component allows the physics engine to control the object.

How to add a Collider Component in Unity?

To add a collider2D component in Unity, we use the Add Component option present in the Physics 2D section of the Box Collider. Select the Gameobject in the scene and click on add component. 
 

Source: Link

Type “collider 2D” or “rigidbody 2D” in the search box and select the component. 

Source: Link

After selecting the component we can see the collider in this way for a polygon:

Source: Link

What are the different types of non-primitive colliders?

Different types of non-primitive colliders are as discussed below:

  1. Compound Colliders - Compound colliders approximate the shape of a GameObject while keeping a low processor overhead.To get further flexibility, you can add additional colliders on child GameObjects. 
  2. Mesh Colliders - They are used to match the shape of the GameObject’s mesh exactly. These colliders are much more processor-intensive than primitive types, so they are used sparingly to maintain good performance.
  3. Static Colliders - Colliders added to a GameObject without a Rigidbody component to create floors, walls and other motionless elements of a Scene are static colliders.
  4. Physics Materials - These are used when colliders interact and their surfaces simulate the properties of the material they are supposed to represent. Although the shape of colliders is not deformed during collisions, their friction and bounce can be configured using Physics Materials.
  5. Triggers - A collider configured as a Trigger does not behave as a solid object and will simply allow other colliders to pass through.

Frequently Asked Questions

How many types of colliders are there?

There are seven types of colliders: box, capsule, circle, composite, polygon, edge, and tilemap.

What is the difference between collisions and colliders?

A collision has information about the impact, such as velocity and points of impact. However, colliders are the objects that made the impact and are used to know whether the gameObjects collided or not. Hence, although the terms of collisions and colliders are inter-related, there is a stark difference between their meanings.

Do we need rigid bodies for implementing collisions?

Yes, we need to use the Rigidbody component for a collision.

Conclusion

This blog introduced the concept of collision and colliders with respect to game development. We learned how to add a collider component to our project and clearly understood the difference between collision and collider. 

Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must have a look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass