Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by btonios · Oct 26, 2016 at 12:15 PM · collisionaudiosourceplayoneshot

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image btonios · Oct 27, 2016 at 10:28 AM 0
Share

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?

avatar image OutOfRam · Oct 28, 2016 at 05:48 AM 0
Share

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!

avatar image btonios OutOfRam · Oct 28, 2016 at 11:56 AM 0
Share

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^^

avatar image OutOfRam btonios · Oct 28, 2016 at 04:20 PM 0
Share

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;
         }
    }

}

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges