Alternative to camera.main.transform.position

My initial intuition about which position the breaking sound should come from was the position where the collision occurs and it seems to work fine. Here’s the code sample, try it for yourselves!

    public void OnCollisionEnter2D(Collision2D collision)
    {
        AudioSource.PlayClipAtPoint(breakSound, collision.transform.localPosition);

        Destroy(gameObject);
    }

I am curious why we would use either Camera.main.transform.position or the colision.transform.localPosition, though.

Since the Block itself is what’s breaking, should it be the source of our audio?

I’m using simply this:

AudioSource.PlayClipAtPoint(breakSound, transform.position);

I’m not sure why we’d use anything else. Am I missing something?

Hi Zephyr,

PlayClipAtPoint works in the 3D space. If you want to achieve a three dimensional sound effect, e.g. the sound is coming from the right or left depending on where the destroyed block is, you could instantiate the AudioSource at the position of the block. If you do not want this effect, instantiate it at the position of the AudioListener, which is usually attached to the Main Camera game object.

1 Like

My answer is to use the block position like you did, to get the sound playing left/right but then use the z-position of the camera to make sure the volume is correct:

In DestroyBlock()

        Vector3 soundPosition = transform.position;
        soundPosition.z = Camera.main.transform.position.z;
        AudioSource.PlayClipAtPoint(audioClip, soundPosition);

Would love to hear the community or @Rick_Davidson’s thoughts (love your sense of humour Rick)

Privacy & Terms