- Home /
The question is answered, right answer was accepted
Script won't detect collision
Hello,
For about a week now i've been working on a 2D physics game where the objects will move according to the tilt of the device its ran on. Im done with physics but now im finishing particles, lighting and sounds. I have this script which will play a random of 3 hit sounds when a cube hits either another cube or a wall. The script doesn't give any warning or errors when the game is started but no sound plays when it collides with the floor. To make sure its not just the sound I added a debug.log in the first line but that never ran.
using UnityEngine;
public class RandomSound : MonoBehaviour
{
public AudioClip[] AudioClips;
public void OnCollisionEnter(Collision collision)
{
Debug.Log("hit"); //check if working
if (collision.relativeVelocity.magnitude > 1)
{
int CP = Random.Range(0, AudioClips.Length);
GetComponent<AudioSource>().clip = AudioClips[CP];
GetComponent<AudioSource>().Play();
}
}
}
Answer by webcam · Jun 25, 2017 at 04:48 AM
If you're using 2D colliders you need to listen with a physics 2D function
public void OnCollisionEnter2D(Collision2D collision)
{
...
}
Follow this Question
Related Questions
Platformer2D Player sticks to platform corners 0 Answers
rigidbody 2d goes through polygon collider 2D 0 Answers
How do I make collisions where both objects are moving but pass through each other? 0 Answers
Issue with checking to see whether the Bounds value's of two game objects equal each other 1 Answer