How to reference (receive) a raycast hit on a collider

If another object raycasts and hits the player’s collider, how do i reference that raycast hit point from the player script? I assume i could get component or reference the script, however the main thing i need to do is to reference the actual hit.point to instantiate something at that location and i haven’t found a method that isn’t ridiculously costly on the system, especially if these raycasts (and instantiates) occur constantly (from bullet fire).

so like if gunfire is the raycasts. And where they hit the player’s collider, i need to instantiate bullet holes. (but they need to be instantiated in the players script, not the attackers from where the raycast is originally initialized).
Hope that makes sense. Thanks!

The cost of telling the player of the hit is minimal compared to the Instantiate(). SendMessage() is the most expensive, and I doubt you would even feel it. A more efficient method is for each enemy to get a reference to the script on the player (GetComponent() drag and drop) in Start() or before the app is run, then just call a function. C# events and delegates are even more efficient.

The Instantiate() is the thing to get around (and even there I would run a test before I abandoned it). Often for mobile, marking bullets is done by dynamically building a mesh where each quad in the mesh represent one decal.

You can simply pass the point to the hit collider.

  1. Create a method on the player to receive a Vector3
  2. Use GetComponent to get the player component
  3. Call you method with the Vector3 from RaycastHit.point

There is also no reason you could not pass the entire RaycastHit if you needed to.

Pseudo code:

// On player
public void GetHit (Vector3 hitPoint){
    // Do something with hitPoint
}

// On raycaster
hit.collider.gameObject.GetComponent<Player>().GetHit(hit.point);