Hi. I cant seem to be able to get my sound to be played after i step into the trigger zone . Used the scripts that ive got from the internet but none works.Ive tried using the audio source by assigning the audio clip to the clip that i want. Pressed that play button and there’s sound coming be it i step into the trigger or not. I do not want that. What i want is that the sound to be played once my player stepped into the trigger zone set by me. any scipts ?
I have a solution for this problem. I have used this script which I have created in a game. I will explain the script underneath the script. The script is in C#. Here is the script:
using UnityEngine;
using System.Collections;
public class MYCLASSNAME : MonoBehaviour {
public float Xmin;
public float Ymin;
public float Zmin;
public float Xmax;
public float Ymax;
public float Zmax;
public AudioClip yourClip
void Start (){
}
void Update (){
if(transform.position.x < Xmax && transform.postion.y < Ymax && transform.postion.z < Zmax) {
if(transform.position.x < Xmin && transform.postion.y < Ymin && transform.postion.z < Zmin) {
audio.clip = yourClip
audio.Play();
else {
audio.Stop
}
}
}
}
Ok, what this script does is it looks at the parameters (X,Y,Z) and check if it the position of the player is in between the parameters and then plays the audio clip which will be set by the variable yourClip. You will have to set the X,Y,Z Min and Max parameters in the unity editor. You will have change MYCLASSNAME to the script name. Hope this helps