How do you display a target at a ray cast hit point?

I'm trying to set it up so that a bull's eye will appear on an enemy on screen if they are caught by the ray cast. I'm thinking of having an empty act as the OnGui, but I need some way to return the hit point's location to it.

It's like setting up a targeting system like this:

Target

Where a target GUI will be placed on the hit.transform. I need to place an empty with a GUI script on it at that point, possibly through a static var.

function Update(){
//Cast a ray cast and if an enemy is in front, it will lock onto them. Draws a line for reference.
var hit : RaycastHit;
if(Physics.Raycast (transform.position, transform.forward, hit, 100)
   && hit.transform.gameObject.tag == "Enemy"){
   Debug.DrawLine (transform.position, hit.point);
       transform.LookAt(hit.transform);

    }

There are straightforward ways to do this.

1) You have the 3D position of the hit, so instead of a GUI target you could move a target object (could be a simple plane or cube) in 3D space to that location.

Since you likely want the target to appear in front of all other objects in the scene you can assign the target's material to the Overlay renderQueue.

You will also want to billboard it so that it always faces the camera squarely, unless it's spherical or symmetrical in a way that means the direction doesn't matter.

2) Your original plan. Call camera.WorldToScreenPoint to get the screen coordinates.

The easiest way to put a GUIText/GUITexture to a particular point on the screen is to position its transform at 0,0 and use its pixelInset variable to adjust its pixel position.

You can pass a parameter of the type RaycastHit to the Raycast. This is what the Reference says:

static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool 

  1. origin The starting point of the ray in world coordinates.
  2. direction The direction of the ray.
  3. distance The length of the ray
  4. hitInfo If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
  5. layerMask A Layer mask that is used to selectively ignore colliders when casting a ray.

Returns

bool - True when the ray intersects any collider, otherwise false. Description

Casts a ray against all colliders in the scene and returns detailed information on what was hit.

function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
var distanceToGround = hit.distance;
}
}

As you can see it's quite simple to get a RaycastHit object. This will contain the collider that was hit. Thus you can check wether it is an enemy or not. Moreover the collider's gameObject's transform component can tell you the enemies position. Additionally the RaycastHit also contains the point that was hit.

Variables

  1. point

    The impact point in world space where the ray hit the collider.

  2. normal

    The normal of the surface the ray hit.

  3. barycentricCoordinate

    The barycentric coordinate of the triangle we hit.

  4. distance

    The distance from the ray's origin to the impact point.

  5. triangleIndex

    The index of the triangle that was hit.

  6. textureCoord

    The uv texture coordinate at the impact point.

  7. textureCoord2

    The secondary uv texture coordinate at the impact point.

  8. lightmapCoord

    The uv lightmap coordinate at the impact point.

  9. collider

    The Collider that was hit.

  10. rigidbody

    The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.

  11. transform

    The Transform of the rigidbody or collider that was hit.

To sum up, you need something like this:

function Update () {
    var hit : RaycastHit;
    if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
        if(hit.collider.gameObject.CompareTag("enemy"))
        {
        //display the Bullseye at hit.point or hit.collider.gameObject.transform.position
        }
    }
}