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 Delmaros · Dec 15, 2013 at 11:13 PM · collision detectionmaterial color

Collision Detection and Colors

I am currently developing a game where i want the player to fall through objects depending on whether they the same color as that object. I have managed so far to disable the collider when the player is a different colour to the object. The problem arises however after the player has fallen through the object. The player continue to falls through the ground despite me enabling the collider once again.

  void OnCollisionEnter2D(Collision2D _Collision)
    {
     
    if(_Collision.rigidbody.renderer.material.color != this.renderer.material.color)
             {
                 _Collision.collider.enabled = false;
                 }
     }
     void OnCollisionExit2D(Collision2D _Collision)
     {
         _Collision.collider.enabled = true;
     }


Thank you in advance

Comment
Add comment · Show 3
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 robertbu · Dec 16, 2013 at 06:43 AM 0
Share

Are you sure you are enabling the collider? Untested, but it seems to be that you might not get an OnCollisionExit2D() call if the collider has been disabled.

I'm assu$$anonymous$$g the above code goes on the object and you are disabling the player. If my theory is right, then maybe you could disable this collider and then use an Invoke() to reenable it some short time in the future.

avatar image iwaldrop · Dec 16, 2013 at 06:46 AM 0
Share

Another workaround to collisions not occurring with a disabled collider is to change the collider to a trigger and monitor the OnTriggerExit call ins$$anonymous$$d.

avatar image Delmaros · Dec 16, 2013 at 12:12 PM 0
Share

The disabling of the collider works fine and the player drops through the platform. I just can't seem to get the collider to re-enable after it has fallen through the platform.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Dec 16, 2013 at 06:38 AM

A couple things wrong here:

A) You're disabling the block's collider, not the players.

B) You're checking everything that the player collides with to see if their color is not equal to the player's. If it's not, then you're disabling that block's collider.

I think you mean to be checking to see if the two colors are the same. In that case you should change your conditional to the following:

 if(_Collision.renderer.material.color == renderer.material.color)

Also, you may wish to be disabling the player's collider, in which case you'd do the following:

 collider.enabled = false;

and not

 _Collision.collider.enabled = false;

Finally, if you elect to toggle the player's collider, and if the 2D stuff is anything like the 3D stuff, there is a parameterless version of OnCollisionExit that doesn't require Unity to generate a bunch of collision data. Try replacing the following:

 void OnCollisionExit2D(Collision _Collision)

with

 void OnCollisionExit2D()
Comment
Add comment · 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
0

Answer by bravo4brandon · Oct 11, 2015 at 12:32 PM

I know I'm very late but the problem could be the fact that once you disable the collider upon entering, the collider can no longer detect the OnCollosionExit because it's disabled . A better way to do this would be to apply the disabling collider to the object you want it to pass through and not the player character itself.

Comment
Add comment · 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

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

18 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

Related Questions

I want to make a character jump and crouch without using boolean 0 Answers

OnTriggerStay working in Editor, but not on Android device 0 Answers

CapsuleCast is detecting the wrong hit normal when colliding with two surfaces 0 Answers

Why is the material switching back after collision? 1 Answer

Checking to see where Roulette ball landed in the wheel 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