Search Unity

[SOLVED] Problem with Two Colliders on One Object

Discussion in 'Scripting' started by Blue Bean Apps, Aug 31, 2014.

  1. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Hey, the game I'm working on has an object that has two colliders (Polygon Collider 2D and Box Collider 2D). I have a script attached to this object that has an OnCollisionEnter2D function. What I want to do is to have the script detect which collider that is attached to the object (either the Polygon or the Box) collides with another collider that is attached to a different object. To be more exact, I want this: If the object's Box Collider 2D starts colliding with a different object's Box Collider 2D, do... In simpler terms, I want it to do a certain event based on which collider 2D (either Polygon 2D or Box 2D) engages in a collision, completely ignoring the collider that is not involved in a collision. I'm not even sure if this is possible. I hope this doesn't sound too confusing, but this is really frustrating me. This is the very last part I need done to fix a problem I've had for about 3 weeks now! Any help will be greatly appreciated! Thanks!

    -- Chris
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    you could check for the component.

    so something like (pseudo code)

    if(GetComponent <BoxCollider> ()) {
    //Do Box Collider Logic.
    } else if (GetComponent <EdgeCollider> ()) {
    // Do Edge Collider Logic
    }
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Personally I wouldn't restrict the TYPE of collider. And instead I'd use layers to define which hit who.

    I'd also split those colliders into two separate GameObjects (children of this root).

    Then just stick each collider on their own layer. And then in the physics settings set which layers collide with what.
     
  4. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Thanks for the help everyone, but neither of your ideas solved the problem for me... well, not directly. smitchell, your idea gave me the idea to try a new idea, which worked out in the end! Funny logic there, but whatever works, right? Thanks for the help! And by the way, smitchell, I can sort of see sharp, but I prefer javascript because it's easier and there's less coding involved, but I like the joke!

    PS: Feel free to drop by the WIP forum tomorrow because I intend to show it off in there! :)

    -- Chris
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Note for that joke... Java is a different language from javascript.
     
  6. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    I realize that, but I figured he might have been referring to Javascript since this is a forum dedicated to Unity, which supports Javascript, but not Java. Java and Javascript are stupid names in my opinion because they are too similar. When I first started coding, about a few months ago, I thought Java was short for Javascript, but then I found they are completely different.

    -- Chris
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I wouldn't say Java is a stupid name. It was named that before javascript even existed. It's not its fault the confusion exists.

    Javascript also wasn't originally called javascript. Instead was called Mocha, then quickly changed its name to LiveScript. Then just like a flash, it changed again, the javascript. It's believed it was done to capitalize on the popularity of Java at the time.

    Though later it changed yet again when it became standardized as 'ecmascript'.

    And with all that... javascript in unity isn't javascript either. It doesn't follow the ecmascript standard. Instead its a language that is inspired by javascript/ecmascript. Its real name is unityscript.