In this tutorial, we will see how to detect collision in unity 3D between different game objects. We will use Collision class to detect the collisions between game objects.
We will use below scene for demo.
We will move player object with keyboard input and check it’s collision with other objects.
Steps to detect collision in Unity 3D
Step 1:
In this tutorial, I will show you how to add and use collision to your particle system.Twitter: that you can play now. Main objective of this blog post is to give you an idea about how to use Collision Detection without Rigid body in Unity. Step 1 Introduction Here, just vertical movement is checked but one can check the other directions by modifying the scripts and adding objects to the scene (or changing cube positions).
Unity On Collision Stay 2d
My first simple tutorial on scene changing when making your game in Unity, Scene changing is really helpful when moving through building's and areas as it he. Want a beginners guide to destroying an object upon collision in Unity3D? Well you found it.In just a couple of minutes I show you how you can destroy and du. Reset scene on collision? Flawlessly finished level from my small Bzzzt game made in Unity. Posted by 2 days ago.
The game object on which collision detection will be triggered should be having Rigid Body and Collider components. For this demo, we will attach Rigid body component to the player game object. Box collider is already available to primitive Cube objects.
Unity On Collision Not Working
Make sure Is Kinematic check box is unchecked.
Step 2:
Attach Collider components to the other game objects. Here we have added Box Collider to Ground and Enemy Game Objects. Rigid Body component is optional. Make sure that “Is Trigger” checkbox is unchecked.
Step 3: Attach below script to Player game object.
Unity Oncollisionenter2d
2 4 6 8 10 12 14 16 18 20 22 24 | using UnityEngine.UI; publicclassCollisionBehavior:MonoBehaviour publicText LogCollsiionEnter; publicText LogCollisionExit; privatevoidOnCollisionEnter(Collision collision) LogCollsiionEnter.text='On Collision Enter: '+collision.collider.name; { LogCollisionStay.text='On Collision stay: '+collision.collider.name; { LogCollisionExit.text='On Collision exit: '+collision.collider.name; } |
Unity On Collision Enter 2d
Our setup is complete. Run the application to see the results.
Collide with specific objects – layers
We can also detect collision for specific set of objects. We can use layers for the same. For this example, we will do the following changes:
Unity How To Detect Collision
- Create three different layers, Ground, Player and Enemy. Apply these layers to respective game objects. To know more about layers, checkout this layer masking tutorial.
- Open Physics settings. (Edit -> Project settings -> Physics)
- There is Layer Collision Matrix in physics settings. Here we can check that which layer will interact with other layers.
With above settings Player Layer Objects with not collide with Enemy layered objects.
Hope you get an idea about to detect collision in unity 3D. Post your comments for queries and feedback. Thanks for reading.
The following two tabs change content below.- Using Transparent Material in Unity 3D - February 8, 2021
- Getting started with UI Toolkit : Unity 3D Tutorial - December 30, 2020
- Using Events in Unity 3D - May 2, 2020