I’m creating a game where the character is in a forest and needs to be led to a barn…
In order for the character to be led to the barn, I was hoping to calculate the distance between the character and the barn
- if distance is > x, it will play an audio piece…
-
- if distance is < x, it will play a different audio piece…
However, with this… it constantly keeps calculating the distance and therefore playing the sound every step (playing start of audio, then replaying start and so on…) - audio does not finish and keeps repeating.
Is there a way I can calculate the distance, store the calculation - play sound, then re-start calculation etc;
Here is what I have so far… any help would be appreciated.
#pragma strict
var thePlayer : Transform;
var Barn: Transform;
var hot: AudioClip;
var cold: AudioClip;
function Start(){
}
function Update(){
var BarnDistance = Vector3.Distance(thePlayer.position, Barn.position);
if(rigidbody.velocity.magnitude == 0);
{
if ( BarnDistance > 100) {
audio.clip = cold;
audio.Play();}
}
{ if ( BarnDistance < 100) {
audio.clip = hot;
audio.Play();}
}
}