Help me!I don’t understand Ray,RayCast,RaycastHit ??
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");
}
}
}
Great answer