- Home /
Position of a Collision
I am using a plug-in that handles collision detection of my sprites in a 2D environment. I would like to access the contact.point in OnCollisionEnter, but cannot seem to find a method to extract the information without calling the OnCollisionEnter. Any thoughts?
Answer by aldonaletto · Nov 20, 2011 at 03:40 AM
Contact points are only reported in OnCollisionEnter/Stay, both occurring in the physics cycle (50 times per second). But if you want to have the contact points available outside these functions, you can make a copy inside them and access the copy in any function:
var contact: ContactPoint; // saved contact point
function OnCollisionEnter(coll: Collision){ contact = coll.contacts[0]; // save the first contact point } Copying the first contact is faster and solves most cases, but you can copy the entire contacts array as well:
var contacts: ContactPoint[]; // contact points saved
function OnCollisionEnter(coll: Collision){ contacts = coll.contacts; // save contact points }
Yeah, that's right. Thanks for helping me clear my head
Okay... not quite there yet. Orthello2D uses OnTriggerEnter to register collision detection. I can only get contacts through OnCollisionEnter. Can I bridge these two functions?
That's bad - OnTriggerEnter only passes the collider hit, not the hit point. Why do you need the collision point? To deter$$anonymous$$e the impact direction?
A little creature attaches itself to a building upon collision. Right now, I just have the creature setup to attach at the position it is located upon hitting the building. Since the collision point is different than the transform.position, the creature has the appearance of being just slightly off from where it collided. So, it's attached, but it looks like it's floating in the air.