Unity3D: Physical rays and unity3d rays
RAY:From a point to a direction, a ray of infinite length is emitted, which will collide with an object with a Collider component in the scene.
Use of rays:
1 public class Test 2 {3 private void Start () 4 {5 if (Input. getMouseButtonDown (0) 6 {7 // emits a Ray from the main camera to the position clicked by the mouse. 8 Ray ray1 = Camera. main. ScreenPointToRay (Input. mousePosition); 9 // emits a Ray in the forward direction of the world axis from its position. 10 Ray ray2 = new Ray (this. transform. position, Vector3.forward); 11 // declare an RaycastHit variable to store the collision information of rays. 12 RaycastHit hitInfo; 13 // check whether the rays collide with the object 14 if (Physics. raycast (ray1, out hitInfo) 15 {16 // execute code 17} 18} 19} 20}
According to the above Code:
HitInfo. point:Coordinates of collision points.
Physics. Raycast ():When this method is used to detect rays, because the method has a lot of heavy loads, you must find out which heavy load method you are using and fill in the correct parameters. Otherwise, errors may occur easily.
Because the rays are invisible, we can simulate the rays by drawing a line to facilitate debugging. The method is as follows:
Debug. DrawRay (Vector3 pos, Vector3 dir, Color c ):Draw a line.
Pos: Start coordinate.
Dir: direction.
C: color.