- Home /
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!
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. :)
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?
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.
Answer by mastermindsdei · Jan 04, 2017 at 08:49 AM
Maybe you can try using this
other.gameObject.GetComponent<Collider>().ClosestPointOnBounds(transform.position);
This works for me perfectly using onTriggerEnter(). Thank you man.
This works perfect for me! For those using OnTriggerEnter2D, ClosestPointOnBounds doesnt exists. So you can use collider2d.bounds.ClosestPoint(otherPos).
Where does that line of code go?
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.tag == "Player")
{
// Explosion Impact
Instantiate (explosion, transform.position, transform.rotation);
}
Replace "transform.position" in your Instantiate call with master$$anonymous$$dsdei's code snippet.
Thank you, I needed this, this week coincidentally!!! Works great!
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. :(
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
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?
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.