• 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
9
Question by Jean-Luc Potvin · Jan 14, 2011 at 09:17 PM · ontriggerenterhitpointhingejointcontact

How to find the point of contact with the function OnTriggerEnter???

Hi,

(sorry for my bad english!)

I'm building a little train game. I have some wagons and a locomotive all on differents positions on the track (not touching each other). When I get the locomotive to touch a wagon, my script (who is attach to the wagon) create (AddComponent) a hinge and attach the hiting object (the locomotive or another wagon) to connectedBody of the hinge. This is working fine. My problem is I need to know where the trigger enter so I can move the anchor of the hinge to the front or the back of the wagon to get realistic movements. (Am I clear???)

Here the code:

var connected : boolean = false;

function OnTriggerEnter (other : Collider) {

if (other.gameObject.tag == "Wagon") {
    if (!connected) {
        var otherBody = other.gameObject.rigidbody; 
        gameObject.AddComponent (HingeJoint);
        hingeJoint.connectedBody = otherBody;
        hingeJoint.axis.y = 1;
        hingeJoint.axis.x = 0;
        // move the anchor of the hinge to the pont of conctact (HELP!!!)
        connected = true;
    }
}

}

Thank you very much for your time and your knowledge!

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

4 Replies

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

Answer by PrimeDerektive · Jan 14, 2011 at 09:24 PM

OnTriggerEnter() does not handle collision information, just detects if there was a collision. So you cannot find the point of collision like you would with OnCollisionEnter().

You could however turn off isTrigger and add a rigidbody component to the object with the collider that you are running OnTriggerEnter() on, and make it a kinematic rigidbody, though. Then it would function just like a trigger, but you could use the collision information in OnCollisionEnter() and get the contact point.

Once you have that, you can get the contact point with OnCollisionEnter like this:

function OnCollisionEnter(collision : Collision){
    var contact : ContactPoint = collision.contacts[0];
        contact.point; //this is the Vector3 position of the point of contact
}

Also, your English is pretty good. :)

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 efge · Jan 14, 2011 at 09:39 PM 1
Share

Rigidbody.is$$anonymous$$inematic ... good idea!

avatar image Jean-Luc Potvin · Jan 14, 2011 at 10:06 PM 0
Share

Thanks for the answer,

I did what you told me and it's working as it was with OnTriggerEnter. Now, can you explain how to get access to that contact point?

avatar image PrimeDerektive · Jan 14, 2011 at 11:46 PM 0
Share

Sure thing, see my updated answer.

avatar image toum · Dec 06, 2012 at 01:29 PM 2
Share
@efge OnCollisionEnter/Stay/Exit are not called if rigidbodies are $$anonymous$$inematics: "Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
avatar image taranasus · Mar 31, 2016 at 07:05 PM 1
Share

Just wanted to add a small comment to this thread of a problem I've had and potentially others may do so as well. I wanted to use OnTriggerEnter ins$$anonymous$$d of OnCollisionEnter because the collision with an object will of course affect the other object in terms of physics.

HOWEVER, if you set one of the object's masses to 0, that will no longer be an issue and I find it worked really well for me :) Hope this helps out someone else reading this.

avatar image mani3307 taranasus · May 11, 2018 at 06:44 AM 0
Share

I think one can't set object mass to zero!

avatar image F-N taranasus · Mar 14, 2020 at 11:20 AM 0
Share
Thanks @tarnasus! This worked for me. Setting to kinematic didn't work for me, because collisionEnter is not called for kinematic rigidbodies. @mani3307, that's true. But you can set it to 0.0001 ;P
Show more comments
avatar image
29

Answer by mastermindsdei · Jan 04, 2017 at 08:49 AM

Maybe you can try using this

 other.gameObject.GetComponent<Collider>().ClosestPointOnBounds(transform.position);




Comment
Add comment · Show 8 · 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 muberti · Jul 23, 2017 at 09:19 AM 0
Share

This works for me perfectly using onTriggerEnter(). Thank you man.

avatar image Schminitz · Oct 13, 2017 at 12:11 PM 0
Share

This works perfect for me! For those using OnTriggerEnter2D, ClosestPointOnBounds doesnt exists. So you can use collider2d.bounds.ClosestPoint(otherPos).

avatar image Hybris_Team · Feb 22, 2018 at 04:03 AM 0
Share

Where does that line of code go?

 function OnTriggerEnter (other : Collider) 
 {
 if (other.gameObject.tag == "Player")            
     {
     // Explosion Impact
     Instantiate (explosion, transform.position, transform.rotation);
     }    
avatar image bigChris Hybris_Team · Oct 31, 2019 at 05:27 PM 0
Share

Replace "transform.position" in your Instantiate call with master$$anonymous$$dsdei's code snippet.

avatar image Hybris_Team bigChris · Nov 10, 2019 at 02:14 AM 0
Share

Thank you, I needed this, this week coincidentally!!! Works great!

avatar image joshuapeacock · Jun 13, 2018 at 06:19 AM 0
Share

This worked great for me! Thanks man!

avatar image DearUnityPleaseAddSerializableDictionaries · Sep 04, 2021 at 12:10 PM 0
Share

The issue is I still want to try find the closest overlapping point between two "large" colliders.

So I can't just use transform.position to approximate the other's collider. Unfortunately the solutions are to set a really small mass and not to use triggers, or approximate it using transform.position. :(

Show more comments
avatar image
3

Answer by luxe001 · Nov 05, 2012 at 01:13 PM

Hi, you can try that, is simple and fast for detect collision coordinate:

 function OnTriggerEnter (other : Collider) 
 { 
   print(other.transform.position);  
 }


G.B

Comment
Add comment · Show 2 · 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 SimasS · Jan 27, 2015 at 09:13 PM 0
Share

luxe001 this was exactly what i needed! thanks mate

avatar image ls97 · May 27, 2015 at 11:19 AM 0
Share

I have a long boxcollider and a car but how do I check if the car is in the center of the boxcollider? I allready have a boxcollider on my car and both colliders are triggers. this is my code that gets the position:

 void OnTriggerStay(Collider col)
     {
 //        Transform linetrans;
 //        linetrans = col.transform;
 //
 //        Vector3 linepos = linetrans.position;
 //        float leftright = linepos.z - this.gameObject.transform.position.z;
 //        //Debug.Log (leftright.ToString ());
 //        Debug.Log (linepos.ToString ());
         Debug.Log (col.transform.position.ToString ());
 
 
     }

but all I get is 0,1,0 all the time, so basicly it says I'm in the same position allways. what did I do wrong?

avatar image
0

Answer by aksh2143 · Jan 06, 2016 at 05:22 AM

i am stuck a bit here. i am not using any rigidbody in my object here. but still i want to find the reflect of the point where my ball collides. How can i get that outside the OnCollisionfunction? is that even possible? Both the ball and my wall has colliders bu t none of them have rigidbodiy. But still i want a reflecting vector.

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

16 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

Related Questions

If collider between two points get this point 1 Answer

Point of contact with player 1 Answer

Using contact point 1 Answer

Prticle collision point 1 Answer

What is the optimal way to get the hit point in a trigger collision? 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