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 /
avatar image
1
Question by Imapler7 · Sep 19, 2017 at 08:35 PM · collisiononcollisionexit

Get the collider that exits from an OnCollisionExit

I have a robot with several parts. The root object has a Rigidbody and a Robot script, each child has a BoxCollider and some children has a Tread script.

I want to know when the treads are in contact with something. The events fire and everythings seems fine except i dont get any contact points OnCollisionExit and i cant find any other way to get which specific collider that exits. All i have OnCollisionExit is MyRobot and the object i exited from but i need the object with the actual collider such as somePart1 or tread2.

If i place OnCollisionExit in the Tread script it does not fire when it contacts the ground since neither of them have a Rigidbody.

My object hierarchy and what scripts they have:

 MyRobot (Rigidbody, Robot)
   somePart1 (BoxCollider)
   somePart2 (BoxCollider)
   somePart3 (BoxCollider)
   tread1 (BoxCollider, Tread)
   tread2 (BoxCollider, Tread)

And my Tread script looks like this:

 public class Tread : MonoBehaviour
 {
     public bool isInContact = false;

     public void Move()
     {
         if (isInContact)
             r.AddForceAtPosition(/* stuff */);
     }
 }

My Robot script looks like this:

 public class Robot : MonoBehaviour
 {
     private void OnCollisionEnter(Collision collision)
     {
         setContact(collision, true);
     }
 
     private void OnCollisionStay(Collision collision)
     {
         setContact(collision, true);
     }

     private void OnCollisionExit(Collision collision)
     {
         setContact(collision, false);
     }
 
     private void setContact(Collision collision, bool value)
     {
         foreach (var item in collision.contacts)
         {
             Tread tread = item.thisCollider.GetComponent<Tread>();
             if (tread != null)
                 tread.isInContact = value;
         }
     }
 }





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
1
Best Answer

Answer by FortisVenaliter · Sep 19, 2017 at 10:20 PM

Yeah, the treads need their own rigidbodies if you want to track collision on them separately. Otherwise they are treated like they are the same object.

I recommend putting Rigidbodies on the treads with FixedJoints to hold them in place to the parent Rigidbody. Then you can call your collision code from the Tread class.

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 Imapler7 · Sep 24, 2017 at 10:15 AM 0
Share

This also has the added effect that there is a little give between the part making the construction not completely rigid

avatar image
0

Answer by Brandon529 · Apr 20, 2018 at 05:57 PM

I have been trying to figure this out for almost 2 months and i finally FIGURED IT OUT. OnCollisionExit is fired on the frame AFTER you exit the collision, so there is no contact information. however you can store your collision by creating a new variable in your script as a Collision.

public Collision LastCollision;

And then you can store your last contact point in that variable from your OnCollisionEnter and OnCollisionStay functions like so public void OnCollisionStay(Collision collision){ setContact(collision, true); LastCollision = collision; } After you've stored your last contact point you can use it on OnCollisionExit as your collision and be able to get the contact information from your last contact(such as normals, impact velocity and all your contact point needs). You can do so even without referencing to your collision on an input variable unless you want to. public void OnCollisionExit(){ setContact(lastCollision, false); } Hope this helps anyone out there having the same problems, and remember, there's always a way. There has to be.

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

111 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 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

Why is OnCollisionExit2D late? 1 Answer

OnCollisionExt not working/ Alternate collision detection 1 Answer

OnCollisionExit not being called 0 Answers

onCollisionExit2d problem, character doesn't get deparented 0 Answers

Does OnControllerCollider have an equivalent to OnCollisionExit? 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