How to get the Normal of a collider for orienting an explosion.
I have a missile with a capsule collider that impacts a space station with a box collider. They are both set to be triggers.
I want to orient the explosion to the normal of the collider on the space station that the missile impacted, at the point of impact. I thought I knew how to do that, but the orientation is not looking right.
The code below is the OnTriggerEnter function on the station being hit. It puts the explosion in the right place, but the orientation doesn't look right. Sometimes I'll be hitting the side of a box collider but the explosion is perpendicular to it. Is the collider normal being updated as it rotates with the object to which it is attached?
private void OnTriggerEnter(Collider other)
{
// normal getter is a ray. Back it out along the path of the missile
normal_getter.origin = other.transform.position - other.transform.forward * 10;
//point it in the direction of the missiles travel at the time of impact
normal_getter.direction = other.transform.forward;
//cast the ray to get the hitinfo. Hitinfo.normal *should* be the normal of the
// collider that is impacted
Physics.Raycast(normal_getter, out hitinfo);
//put the explosion on the point where impact was detected
explosions[hits].transform.position = other.transform.position;
//orient the explosion to the normal of the collider face
explosions[hits].transform.rotation = Quaternion.LookRotation(hitinfo.normal, Vector3.up);
explosions[hits].transform.parent = gameObject.transform;
hit_points[hits] = 1;
explosions[hits].SetActive(true);
hits += 1;
if (hits == 5) hits = 0;
}
Answer by DrJBN · Mar 10, 2021 at 09:06 AM
I figured out my problem- Object A was hitting Object B and I wanted the normal on Object B where A had hit. I was casting my ray from a point along object A's path to object B, but Object A was still setting there on Object B, so my ray was hitting Object A and returning it's normal instead of object B.
Your answer
Follow this Question
Related Questions
Not able to raycast on image 1 Answer
Shooting through walls 0 Answers
Raycast with image over collider 0 Answers
raycast going through walls 0 Answers
How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer