Hi, I want to play the audio of an object when my player hits it. For example, my player when hits the object, the sound of that object plays 1 time and my player collects 1 point from it. I know the point collection but the sound ain’t playing when my player hits it.
Simplest way is to call AudioSource.PlayClipAtPoint().
@SaintTippu you can simply attach the audio source to the player & change the clip as needed when collide with something @aballif to avoid playing desabled AudioSource when deactivating object that hold the sound :
public AudioClip coin;
public AudioClip wall;
AudioSource audio;
void Start(){
audio = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision other){
if(other.gameObject.tag == "coin"){
audio.clip = coin;
audio.Play();
}
if(other.gameObject.tag == "wall"){
audio.clip = wall;
audio.Play();
}
}
& make sure objects have collider attached & Rigidbody so colliders work fine & if it’s trigger colliders use OnTriggerEnter & if it’s 2D project use OnCollisionEnter2D or OnTriggerEnter2D & lastly you can vice-versa because you can’t play desabled AudioSource.