I’ve got a game where an important part of the game is the sounds that play when my FPC collides with an object. Different sounds are played depending upon the tag of the object being collided with, and I’ve put the various code elements below.
private var characterHit : boolean = false;
private var characterTimer : float = 0.0;
var josh01Sound : AudioClip;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if (hit.gameObject.tag == "josh" characterHit == false)
{
audio.PlayOneShot (josh01Sound);
characterHit = true;
}
function Update (){
if (characterHit){
characterTimer += Time.deltaTime;
}
if (characterTimer > 3){
characterHit = false;
characterTimer = 0.0;
}
This works fine whilst my FPC continues moving through the game as it should.
The problem occurs if the FPC is stopped whilst the FPC is in contact with a collider.
The sound plays, and 3 seconds later it plays again, and 3 seconds later the same sound plays again, etc. etc.
The 3 second delay is fine whilst the player moves, as by that time he’s moved from collider to collider causing a different sound.
But how do I get the sound to only play once if the FPC stops whilst actually on a collider?
My guess is you should save the last collided object in a var and, before playing the sound again, checking if the current collision is happening against the same saved object. If so, do not play the sound.
man i’m not home to debug it… u just debug it yourself, i just change it the way it should
private var characterHit : boolean = false;
private var characterTimer : float = 0.0;
private var soundPlayed:boolean=false;
private var soundPlayedTimer:Time=Time.time;
var josh01Sound : AudioClip;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if (!soundPlayed/mean sound didn’t played/ hit.gameObject.tag == “josh” characterHit == false)
{
soundPlayed=true;//to check if sound played
soundPlayedTimer=Time.time;// since we are using OnControllerColliderHit, we hit with multiple object so we have to check it over deffined time if we hit with that object
audio.PlayOneShot (josh01Sound);
characterHit = true;
}
else if(SoundPlayed!=true soundPlayedTimer<Time.time+1.0f//delay on desire time to functionallity back to normal, better put less value than 1sec)
{
soundPlayed=false;
}
function Update (){
if (characterHit){
characterTimer += Time.deltaTime;
}
if (characterTimer > 3){
characterHit = false;
characterTimer = 0.0;
}
This does what I asked. It allows the sound to only be heard once, so it prevents the sound being heard repeatedly when the FPC stops on the collider.
My problem now … The FPC sometimes returns some time later to the same object, but the sound doesn’t then work as it should.
What I need is a combination of the two ideas above. To stop the sound if that sound was the last one to be made, but allow the sound if a different sound was the last one heard.
private var characterHit : boolean = false;
private var characterTimer : float = 0.0;
var josh01Sound : AudioClip;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if (hit.gameObject.tag == "josh" characterHit == false)
{
audio.PlayOneShot (josh01Sound);
characterHit = true;
}
function Update (){
if (characterHit){
characterTimer += Time.deltaTime;
}
if (characterTimer > 3){
characterHit = false;
characterTimer = 0.0;
}
to
private var characterHit : boolean = false;
private var characterTimer : float = 0.0;
private var soundPlayed : String;
var josh01Sound : AudioClip;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if (hit.gameObject.tag == "josh" characterHit == false soundPlayed !="josh01")
{
audio.PlayOneShot (josh01Sound);
characterHit = true;
soundPlayed = "josh01"
}
function Update (){
if (characterHit){
characterTimer += Time.deltaTime;
}
if (characterTimer > 3){
characterHit = false;
characterTimer = 0.0;
}
This plays the sound the first time the collider is hit.
Does not play the sound if the FPC stops on the collider or re hits the collider before it has hit a different one.
Does play the sound if it hits the same collider, provided it has hit a different one before hand.
Exactly what I wanted.
Thank you to you both for pointing me in the right direction.