Home > Mobile >  OnCollisionEnter not triggering audio clip
OnCollisionEnter not triggering audio clip

Time:03-27

I am trying to make it so that when the 'Player' collides with the ball, an audio sample triggers. I cant see why the trigger would not go off looking at my code and inspector.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerSFX : MonoBehaviour
{
    private AudioSource audioSource;
    public AudioClip kicked;

    void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            audioSource = GetComponent<AudioSource>();
            audioSource.clip = kicked;
            audioSource.Play();
        }
    }
}

Inspector view of ball

Colliding the ball and the player in the scene should trigger the audio clip but it does not.

CodePudding user response:

Try using audioSource.PlayOneShot(kicked); instead of audioSource.clip = kicked; audioSource.Play();

CodePudding user response:

I have tested your code and inspector. Those work fine. Make sure Collider and Rigidbody components have been added to the target game object (the game object with the Player tag) and is trigger is not checked at the Collider component. also check the tag of the target game object.

  • Related