Unity On Collision



  1. Unity On Collision Not Working
  2. Unity On Collision Exit 2d
  3. Unity Oncollisionenter2d
  4. Unity On Collision Destroy Object
  5. Unity On Collision Stay

A really common situation in lots of different games to play a sound when something is hit, that is, a collision happened. Also really common is that this collision results in one of the two objects being destroyed.

Unity On Collision Not Working

I've been there too, we try several things but don't understand why the sound only clicks and pops or even worse, a total silence after the hit.

Destroying objects on collision Destroying other objects that collide with the main object To destroy an object on collision within the using ty software, you have to use some form of the void OnCollisionEnter method. For 2D games you need the void OnCollisionEnter2D method and the void OnCollisionEnter method for 3D games.

  1. Now in unity, you can do it through detection of the collision. You have to add a rigid body to one of the colliding bodies and colliders to both game objects. Using OnTriggerEnter and OnColliderEnter, on both bodies.
  2. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions. Using UnityEngine; using System.Collections; public class ExampleClass: MonoBehaviour AudioSource audioSource.
  3. Houston Community College is one of the largest institutions of higher education in the USA with more than 70,000 students each semester, including more international students (nearly 6,000) than any community college in the country.

There are two very commons reasons for this to happen:

  1. The gameobject containing the AudioSource is destroyed.
  2. The collision method isn't called.

Each of these reasons has a different way to be diagnosed and fixed. Let's start with the first one that seems to be the most common.

Unity On Collision

The gameobject containing the AudioSource is destroyed

If you're sure that your collision code is being called, because you have proof, either via debug logging or because it has triggered any other thing in your game (particle instantiation perhaps?), then it's really likely that this is the reason.

The Unity editor might also log that 'Can not play a disabled audio source'.

You can solve this problem easily by using a method created just for this issue, the PlayClipAtPoint. This method is going to create an empty gameobject which the engine itself will be responsible for destroying after the sound finishes.

Unity On Collision Exit 2d

This solution has a gotcha, though. You can't redirect the output to a specific Audio Mixer Group using it nor change any of the regular AudioSource parameters. If you need to do that there are at least two solutions. The first alternative would be to create your own PlayClipAtPoint method in which you pass the Audio Mixer Group and return the AudioSource so you could change the other parameters:

The calling function could also change the outputAudioMixerGroup property, but it's such a common task that I think it's a good idea to have this as a parameter.

And the second alternative is to create this object right in the prefab, detach it from the parent before destroying it, and later destroy the detached gameobject. One advantage of doing it this way is that you can use the Unity Editor to tweak the audio parameters of the AudioSource instead of doing it by script.

Unity On Collision

Sounds complicated but it's rather easy to understand once exemplified.

prefab hierarchy
audio source setup

Unity Oncollisionenter2d

If you use PlayDettaching on different types of components you could create an extension method instead of repeating this code. See the official unity tutorial on extension methods.

Collision Method isn't called

The diagnosis is generally made using logs. If you don't see anything in your logs after the collision happens it could be happening (not happening) because:

  1. There's no Rigidbody component on at least one of the game objects
  2. Even with a Rigidbody, there's no non-kinematic Rigidbody attached
  3. Colliders of one of the two objects are disabled or don't exist
  4. Method (OnCollisionEnter, OnTriggerEnter, ...) is named incorrectly, check the capitalization.
Destroy object on collision unity

There are more reasons, but generally, the culprit is one of these four. Fix those and you should be able to hear your sound effect. Just be careful to fix what's described here and forget about what's explained in the beginning of the article, that is, the collision is detected but the AudioSource is destroyed right at the moment the sound starts to play.

If you're still not able to hear your sfx reach me on twitter or by email.

Solved your collision problem? Want to receive more tutorials like this in your inbox? Be sure to subscribe!

Unity On Collision Destroy Object

In the gaming world, we often may need to detect the collision between two objects. For example, in many games, we attack our enemies with the bullet. Our enemies destroy in the game because there is a collision detection between our enemy and bullet.

Of course, you may realize that detecting collisions is a quite important part in game development. So, in this tutorial, I am going to show you how to detect collision between two objects so that you can apply it to your game development project.

Enter

Here we are going to detect collision with a specific object in Unity 3D with C# programming.

The Unity game engine already provides the collision event API or function which is OnCollisionEnter. We can use it in our C#. Below is the syntax to perform and execute the code after the collision:

Inside the OnCollisionEnter function, we can perform our task.

For example, if we want to destroy a particular game object after the collision, we can do it just by detecting the object by its tag name. So first we have to set a tag for our object which we want to destroy.

You can easily set a tag for that specific game object just by selecting it, Go to the inspect and select tag drop-down box. Then from the drop-down list, click on Add Tag…

See the picture below to know where the tag option is available:

After we set a tag for the object, we can detect the specific object by its tag name. We will first check if our game object has the specified tag name and then destroy it on Collision with another Object.

Below is our C# code to detect collision with a specific object in Unity:

Unity On Collision Stay

In the above code, we first check if the object has the tag name with the if statement:

Inside the statement, we have passed the code to destroy the object with the tag name:

Unity

Destroy(targetObj.gameObject);

So we have successfully able to detect collision with an object by its tag.

Leave a Reply





Comments are closed.