Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by danlinenberg · Jun 29, 2013 at 10:18 AM · collisionswitchcase

Collision detection with switch/case

Hi, I'm trying to change a certain variable depending on what type of object my character is walking on. to do that I've used a collision function with switch/case. Problem is - when my character walks on a certain object, it get stuck on that case statement and the variable doesn't go back to its default value.

  function OnCollisionEnter(collision : Collision) {
      var obj=(collision.gameObject.name);
     switch(obj){
         case("EndingIndicator"):
             Debug.Log("Ended");
             break;
         case("Bouncy_floor"):
             isBounce=true;
             break;
         default:
             isBounce=false;
             break;
 
     }
 
  }

What am I doing wrong here? (I tried also naming the "default" object by name but that didn't work)...

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
0

Answer by Em3rgency · Jun 29, 2013 at 11:10 AM

You should read a but how a switch works... The default case activates only when none of the other cases are met. Meaning in your code, if there is a collision, but the object is none of the specified names, it defaults. What you want is for the value to go false when there no longer is a collision.

Use onCollisionExit() for that.

Comment
Add comment · Show 2 · 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 danlinenberg · Jun 29, 2013 at 11:28 AM 0
Share

But it's still in collision when it's in default. This about it this way - It's a platform where the character is always walking across a certain object, and the default is the object that has no special characteristics. Plus as I said, I tried na$$anonymous$$g it specifically but that didn't work either.

avatar image Em3rgency · Jun 29, 2013 at 12:46 PM 0
Share

Its supposed to be in collision when default. Your switch activates ONLY when there is a collision. As I said, you don't understand how the switch works.

Look, you need something like this...

 Update()
 {
   if(colliding)
     isBounce=true;
   else
     isBounce=false;
 }
 
 OnCollisionEnter()
 {
   colliding = true;
 }
 
 OnCollisionExit()
 {
   colliding = false;
 }

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

16 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

Related Questions

Syntax of switch + case insensitive? 2 Answers

Operator '==' cannot be used! 2 Answers

Saving drop down menu selection 1 Answer

Destroying Gate by hitting a switch 1 Answer

add case in swicth, from editor unity 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