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
6
Question by Juice-Tin · Jun 28, 2014 at 06:30 PM · collidercollidersclimbing

Distinguish between multiple box colliders?

I have a child object called LedgeDetection with 3 Box colliders. The plan is if the bottom 2 are touching a wall bot the top is not, it's a ledge that can be grabbed and climbed onto.

alt text

The problem is when touching a wall, all 3 fire OnTriggerStay at once and it's impossible to tell them apart. I can't think of a good method of doing this. Should I check their local coordinates to distinguish them? Put them all on separate child objects with separate scripts containing OnTriggerStay?

Or am I just doing this completely wrong and there's an easier/better solution?

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

2 Replies

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

Answer by Denvery · Jun 28, 2014 at 08:28 PM

Hello, Juice-Tin! As for me there is 3 ways to distinguish the colliders.

  1. Yes, you can put them on separate child game objects and create new scripts with OnTriggerStay function. It will be useful especially if you are planning to use their separately in future and to implement the other functions (`OnStart, OnTriggerEnter` etc.)

  2. May be more elegant and shorter way. Create 3 public fields in your current script (where you implement OnTriggerStay now):

    public BoxCollider collider1;

    public BoxCollider collider2;

    public BoxCollider collider3;

These girls will appear in the inspector. Then, assign your attached colliders for these fields. So you can distinguish the colliders in OnTriggerStay in the manner:

 private OnTriggerStay (Collider other)
 {
 
    if (other == collider1) ....
 
 }

And please do not check local coordinates to distinguish because it is straight way to non-modifiable code:-)

Hope this answer will help you a bit:-)

Comment
Add comment · Show 11 · 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 Juice-Tin · Jun 29, 2014 at 12:03 AM 0
Share

Hah, public variables. How did I not think of that! Thanks, I feel silly now lol.

avatar image Denvery · Jun 29, 2014 at 10:37 AM 0
Share

Sorry that I described so detailed, it was enough to write one word "public var" ;-)

avatar image rad1c · Oct 10, 2016 at 11:32 AM 2
Share

I know this was 2 years ago, but the 3rd way is? :)

avatar image Olamire rad1c · Mar 31, 2017 at 09:19 AM 0
Share

feeling the same way

avatar image guitarjorge24 rad1c · Nov 01, 2019 at 02:05 AM 0
Share

$$anonymous$$aybe it was to create an array of BoxColliders and use GetComponents<BoxCollider>(); to initialize the array?

avatar image Olamire · Mar 31, 2017 at 08:41 AM 0
Share

i have the same issue when i use- if (Collision==BoxCollider1), it doesn't work. i'm guessing it's because collision is referring to the other game object, therefore i cannot distingusih between BoxCollider1, 2 or 3. Please help

avatar image Denvery Olamire · Mar 31, 2017 at 09:24 AM 0
Share

What is Collision? where is this object from?

avatar image Olamire Denvery · Mar 31, 2017 at 09:34 AM 0
Share

my sprite has two colliders one for the enemy to know when its in range and the other for the player to take damage and pickup items. i created an empty child gameobject and put in the range test collider and tagged it as range but still Ontriggerenter2D still activates for the child object. i need a solution

avatar image angelsm85 · May 28, 2018 at 03:28 PM 0
Share

Hi @Denvery ! Can you help me to solve this similar problem? https://stackoverflow.com/q/50564752/6439069

avatar image rad1c angelsm85 · May 28, 2018 at 04:14 PM 1
Share

Did you mark the collider to be a trigger one in the Editor? If yes, and the script is enabled (this.enabled == true) so $$anonymous$$onoBehaviour calls fire, add some debug code lines for other.name, other.gameObject, etc to see if the event is called at all and if it was, what Collider detected the collision. BTW your "if (other == cCollider) {" will never be true if cCollider is not a trigger one, and that's what I assime from your description. As OnCollisionEnter != OnTriggerEnter.

Show more comments
avatar image
3

Answer by Lylek · Jun 28, 2014 at 09:17 PM

Have you tried raycasting, instead? You can have 2 child objects as transform vars to reference. Then raycast forward from their positions. If the bottom one hits something, but the top one doesn't, then climb.

 var object1 : Transform;
 var object2 : Transform;
 var hit1 : RaycastHit;
 var hit2 : RaycastHit;

 function FixedUpdate() {
     //Projects a raycast from object1 forward, 3
     if(Physics.Raycast(object1.transform.position, transform.forward, hit1, 3)) {
         //If first raycast hits, raycast 2nd
         if(Physics.Raycast(object2.transform.position, transform.forward, hit2, 3)) {
             //if 2nd hits, you're at a wall
             Debug.Log("Wall");
         }
         else {
             //if 2nd does not hit, climb
             Debug.Log("Climb");
         }
     }
 }
Comment
Add comment · Show 1 · 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 Juice-Tin · Jun 29, 2014 at 12:06 AM 1
Share

Yeah, I'm thinking I might have to use Raycasting anyway.

With Raycasting his lefT+right side I'll be able to find the angle of the object, so I can make him face it perfectly. Colliders will tell me an object is there, but not the angle it's at.

Thanks!

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

12 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

Related Questions

Script is only working when slowing down timescale 0 Answers

Roll a Ball can't pick up game objects in the build? 0 Answers

(Light enabled colliders) given a surface, how to make a collider such that only the portions of the surface in contact with a light source are activated?(Game example included) 0 Answers

Character Controller won't collide with anything 1 Answer

3D game: Pickups don't disappear when player touches them. 4 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