0
votes

Note: It's a 2D Game

I'm trying to get an audio clip to play when the Character comes into contact with an object that has a Box Collider around it.

I've tried OnTrigger/OnCollision methods but neither are playing any sound. I've also tried many solutions online but still no sound on collision. My Clip works on Awake, but not as intended.

Checklist:

  • The Player has a "Player" tag.
  • The Player has a Rigidbody 2D and a Collider 2D
  • The Oject Collider has 'Is Trigger' ticked
  • The Object has an AudioSource
  • The Object has an Audio Script
  • The Object has a Box Collider 2D for the collision around it
  • The Audio Clip is attached to the AudioSource component

Here's the current Script (Attached to Object): I'd be very thankful!

public class Audio : MonoBehaviour 
{

    public AudioSource audioClip;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            audioClip.Play ();
        }
    }
}
1
The box collider of the object is marked as trigger you mean? Is that collider the same size as object or bigger (it should be bigger)? - Gunnar B.
Yes I did mean the collider around the object, sorry! And yes, the collider is wider than the object itself, which is a sprite. The collider is 2x the size of the object itself. As I want the clip to play as the Player enters the collider space. - user6403450
Is this 2D or 3D? There are different versions of collider, rigidbody and the OnTriggerX/OnCollisionX functions for 2D and 3D. - Gunnar B.
It is a 2D platformer. - user6403450
Ok, you changed your stuff to use 2D components, but how about OnTriggerEnter? Do you use that or OnTriggerEnter2D? - Gunnar B.

1 Answers

0
votes

In order to find out what is wrong, you can start by simplifying the problem.

You can start by doing something simple in OnTriggerEnter2D, that you know will work, such as a Debug.log

void OnTriggerEnter2D(Collider2D collision)
{
    Debug.Log(this.gameObject.name+ " collided with "+collision.gameObject.name)
}

This way, you will know if it's the collision or the audio that's the problem.

It may be, that the collision works, but you are unable to hear the audio because of wrong settings or some other minor detail you have missed.