Locate where raycast was hit

Updated on March 7, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on March 5, 2017

I have a first person player, and there’s shooting using raycast and I wanted to detect/locate where i got hit with the raycast like in the front or back. Im gonna make a damage indicator to know where i got hit. how do i do this?

  • Liked by
Reply
3 on March 5, 2017

If you want to know if you hit the player in the back, as opposed to the front, then you would probably want to have two different colliders on the player, one for the fron  and then one for the back.

Helpful
on March 5, 2017

well i wanted it to be more advance like the exact direction of the point of hit

Devoted
on March 7, 2017

then you can use the collision.normal to figure that out.

void ONCollisionEnter(Collision col){
  // col.transform.position gives you the position in world space where you hit.
  // col.gameObject gives you the gameObject you hit
  
  //to figure out the contact points themselves (I think across multiple colliders)...
  foreach (ContactPoint contact in collision.contacts) {
    print(contact.thisCollider.name + " hit " + contact.otherCollider.name);
    Debug.DrawRay(contact.point, contact.normal, Color.white);
  }
}

for more info

Helpful
on March 7, 2017

thank you i will try this

Show more replies
  • Liked by
Reply
Cancel