Check if tag is the same as the previous one
Hello,
I'm actually making a system where when my ball touch the ground, a hit sound is playing. I'm using this piece of code:
void OnCollisionEnter(Collision hit)
{
if(hit.gameObject.tag == "Floor")
{
hitball.volume = speed/(maxSpeed);
hitball.pitch = speed/(maxSpeed);
hitball.PlayOneShot(ballhit);
}
}
It works perfectly, but my problem is that my maps are built with blocks, so everytime I reach a new block, the sound is playing. I so had the idea to check what's the previous tag so the sound doesn't play when the next tag is the same. But that's what I'm searching for, and I don't really know how to do it. So, can someone explain me how can I get it to work?
Thanks.
Answer by OutOfRam · Oct 27, 2016 at 02:25 AM
Hello there,
This should be pretty easy, what you need to do is make a string variable to hold the tag and have it only change inside your if statement when ever a new object is touched, then that var will always have the most recent tag but lagged slightly allowing you to do this:
private string tempTag;
void OnCollisionEnter(Collision hit)
{
if(hit.gameObject.tag == "Floor" && hit.gameObject.tag != tempTag)
{
hitball.volume = speed/(maxSpeed);
hitball.pitch = speed/(maxSpeed);
hitball.PlayOneShot(ballhit);
tempTag = hit.gameObject.tag
}
}
This addition to the code will make it only fire when the tag differes from the previous tag(also if tag == Floor, I don't know if you want to remove that part or not).
I hope this helps!
It worked perfectly, thanks! But I'm here with another question: I don't think that my way of doing is right, and I would like to ask you if there is another way to only play a sound when I hit the ground (no matter the block/tag), without playing when reaching each block?
Try this,
private bool inAir;
void update()
{
if (ball.transform.position.y > "whatever y pos would constitute the ball being off the ground")
{
inAir = true;
}
}
void OnCollisionEnter(Collision hit)
{
if(hit.gameObject.tag == "Floor" || hit.gameObject.tag == "other ground related tags")// Add in all of them in this fashion
{
if(inAir)//This will check if the ball was in the air prior to its hitting the floor
{
hitball.volume = speed/(maxSpeed);
hitball.pitch = speed/(maxSpeed);
hitball.PlayOneShot(ballhit);
inAir = false// This will make the ball need to regain height before it can hit again
}
}
}
I did not write this in the editor so there may be some slight errors, please let me know if you need anything else!
Hi, thanks again for the reply!
Sorry to say that it can't works, because the ball isn't staying at the same y position. I had the idea to get the block's position to set a "floor" position and then tell in script that if y != floor, then inAir = true but it seems to be a wrong way since when I reach an uphill block, it'll won't work and play the sound infinitly... Anyway, do you have another idea? :D I was hopping that a collision with another collider without searching name/tag was available, but seems it's not^^
you could always try putting an empty with a collider set to isTrigger above each ground block, then set inAir to true using the on trigger enter function
OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "BallTag")
{
inAir = ture;
}
}
void OnCollisionEnter(Collision hit)
{
if(hit.gameObject.tag == "Floor" || hit.gameObject.tag == "other ground related tags")
{
if(inAir)//This will check if the ball was in the air prior to its hitting the floor
{
hitball.volume = speed/(maxSpeed);
hitball.pitch = speed/(maxSpeed);
hitball.PlayOneShot(ballhit);
inAir = false;
}
}
}
Your answer
Follow this Question
Related Questions
Collision to change Pitch 0 Answers
Im trying to turn off a sound when i leave a trigger collider 0 Answers
Overriding audio priority with PlayOneShot()? 0 Answers
How can an gameobject make a sound when touching the player? 1 Answer
I'm trying to play a sound OnCollisionEnter, but something isn't working right. Can you help me ? 0 Answers