- Home /
The question is answered, right answer was accepted
I don't understand Ray,RayCast,RaycastHit ??
Help me!I don't understand Ray,RayCast,RaycastHit ??
I would recommend to read the docs.
Ray is just an entity representing a line with a start point and a direction but no end.
Raycast is the method you use to cast a ray (or a vector) from a point in a direction with distance (or not) and then reports if the ray hits something.
The RaycastHit is the structure you use in the raycast method to store info on the collision.
So you call Raycast with a ray and if the ray hits something then you store the info in the raycast hit struct.
Answer by aldonaletto · Sep 07, 2013 at 08:31 PM
The ray is a geometric creature: a line starting at some point (the origin) and following indefinitely in some direction. Physics.Raycast is a test that verifies whether some collider is hit by the ray specified, and RaycastHit is a structure that Raycast fills with info about the first object hit by the ray - it has invalid values if nothing hit, thus only read it when Physics.Raycast returns true. A typical use for Raycast is to detect which object was clicked:
function Update(){
if (Input.GetMouseButtonDown(0)){ // if left mouse button pressed...
// get ray starting at camera position and passing through the mouse pointer:
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit; // allocate variable RaycastHit
if (Physics.Raycast(ray, hit)){ // if something hit...
print("Clicked on "+hit.transform.name); // print its name
} else {
print("Nothing hit");
}
}
}
Follow this Question
Related Questions
Raycast is always returning 1 (for normals) 1 Answer
A node in a childnode? 1 Answer
Something fishy with RaycastHit 1 Answer
Rays do not detect terrain and prefabs do not spawn where wanted 1 Answer
RayCasting from camera to player 1 Answer