c# - Get ContactPoint2D/contact normal from the OnTriggerXXX function -
i trying force unity not change speed , direction of objects after collision. need oncollisionenter2d event, don't need unity automatically change speed , direction; want manually.
i tried use code dont work (automatically collision still works):
private void oncollisionenter2d(collision2d collision) { fallvector = getrigidbody.velocity.normalized; fallnormalvector = collision.contacts[0].normal; var reflectedvelocity = vector3.reflect(fallvector, fallnormalvector).normalized; getrigidbody.velocity = reflectedvelocity * 3; } i told mark gameobject istrigger , use ontriggerenter2d other question uses collider2d instead of collision2d , can't contact normal that.
how can contactpoint2d or contact normal collider2d in ontriggerenter2d function?
edit:
after reading answer below, wrote code below, getcontacts() returns (0.0, 0.0) in elements , did wrong?
both objects of collision istrigger = true. (unity version 5.6.0f3)
contactpoint2d[] contacts = new contactpoint2d[2]; void ontriggerenter2d(collider2d collision) { //get contact points , save contacts array variable collision.getcontacts(contacts); vector3 normal = contacts[0].normal; getrigidbody.velocity = vector3.reflect(getrigidbody.velocity, normal); }
i can't use ontriggerenter2d - have no collision2d define contact normal
the normal variable contactpoint2d structure , can translate oncollisionenter2d code ontriggerenter2d , still able retrieve information collider2d.getcontacts function.
you must have unity 5.6 , older in order use function. please don't "it doesn't work" if don't have version because shouldn't. need update feature.
with collision:
void oncollisionenter2d(collision2d collision) { vector2 normal = collision.contacts[0].normal; vector2 point = contacts[0].point; } the trigger equivalent:
//variable hold contacts contactpoint2d[] contacts = new contactpoint2d[2]; void ontriggerenter2d(collider2d collision) { //get contact points , save contacts array variable collision.getcontacts(contacts); vector3 normal = contacts[0].normal; vector2 point = contacts[0].point; } make sure mark colliders triggers first, said in other answer. thing needs changed inside code in function fallnormalvector = collision.contacts[0].normal;.
Comments
Post a Comment