- Home /
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:
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);
}
Answer by Bampf · Nov 18, 2010 at 02:11 PM
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.
Answer by lhk · Oct 30, 2010 at 07:57 AM
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
- origin The starting point of the ray in world coordinates.
- direction The direction of the ray.
- distance The length of the ray
- hitInfo If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
- 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
point
The impact point in world space where the ray hit the collider.
normal
The normal of the surface the ray hit.
barycentricCoordinate
The barycentric coordinate of the triangle we hit.
distance
The distance from the ray's origin to the impact point.
triangleIndex
The index of the triangle that was hit.
textureCoord
The uv texture coordinate at the impact point.
textureCoord2
The secondary uv texture coordinate at the impact point.
lightmapCoord
The uv lightmap coordinate at the impact point.
collider
The Collider that was hit.
rigidbody
The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.
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
}
}
}
I know how to cast a ray. I need to take the position and shift it to another static variable to set up so that a transform can position itself there.
How do I set the GUI to Target the position of the enemy?