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 agiro · Aug 21, 2016 at 05:44 PM · collisioncollidersphysics2d

[C#][Physics2D] multiple colliders and OnCollisions on one gameobject

Greetings, I understand this has been asked but that post was old. I have a gameobject and I want it to have two colliders, (either on same GO or on a child GO) and what is more important, I want to use ONE script, that can distinguish between the two colliders. Is this possible? Regards

Comment
Add comment · Show 7
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 TBruce · Aug 21, 2016 at 06:01 PM 0
Share

This is done all the time. For example you have a 2D sprite that has 3 colliders one for the head, one for the feet and one for the body.

But you need to provide more details (how will you be using each collider, what collision functions will you be using - OnCollisionEnter2D, OnTriggerEnter2D).

Any code that you have would be helpful as well.

avatar image agiro · Aug 21, 2016 at 06:07 PM 0
Share

A regular saint you are here :D I'm making a double jump trampoine. This means that I have two box collider 2d-s, one lower than the other. The reason for this is that as the player enters the first one, they can press the jump button or not. When s/he reaches the second one, the lower (second) collider adds a force upwards (say Vector2.up * jumpSpeed) or if the jump was pressed, adds double force up. I don't want to make 2 scripts for this if no necessary. my code so far, nothing complicated:

 void OnTriggerEnter2D(Collider2D other)
 {
 
     if (other.gameObject.CompareTag("Player"))
     {
         if (!alreadyUsed)
         {
             Rigidbody2D otherRB = other.gameObject.GetComponent<Rigidbody2D>();
             otherRB.velocity = new Vector2(otherRB.velocity.x, 0.0f);
             otherRB.AddForce(Vector2.up * CharacterController2D.jumpForce);
             alreadyUsed = true;
         }
     }
 }
 
 void OnTriggerExit2D(Collider2D other)
 {
     alreadyUsed = false;
 }
 
 public class TramboulineDoubleJump : $$anonymous$$onoBehaviour
 {
     void OnTriggerEnder2D(Collision2D other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
 
         }
     }
 }

please note that I have a parent GO that has the script (that is the upper class, named Trambouline. I just made the lower class to distinguish or what. The upper collider is on the child of the actual GO. the lower collider that actually makes the jump is on the GO itself.

avatar image TBruce agiro · Aug 21, 2016 at 06:48 PM 0
Share

I needed to reformat your comment to be more readable, but to do that I had to make it an answer first.

avatar image agiro TBruce · Aug 21, 2016 at 06:50 PM 0
Share

Uhh sorry, and I converted it back to a comment. The script is really no big deal, all in all I need just if one can have 2 OnTriggerEnter2Ds for 2 different colliders using 1 script.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

86 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

Related Questions

I want my player to walk through walls 1 Answer

RigidBody Collisions Detect 0 Answers

Destroy GameObject inconsistent with Instantiated children 1 Answer

Making my walls disappear by touching the floor 0 Answers

Circle Collider Stuck to Box Collider 1 Answer


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