Game developers can use ray casting for actions like aiming, identifying lines of sight, and measuring distances. There are many overloads of raycast in unity. Now show the most commonly used methods in 2
BOOL Raycast (Vector3 origin, Vector3 direction, float distance, layermask mask);
Parameter interpretation
Origin: The starting position of the light
Direction: direction of Light
Distance: The distance the light travels (can be null)
Mask: determines which layer the light will hit (nullable)
If you want to determine if there is an object before the camera, you can use the following code
void Update () {if (Physics.raycast (Transform.position, Transform.forward, ten)) {print ("There is something in Front of the camera! ");}
Note This code is placed in the camera
Another
BOOL Raycast (Vector3 origin, Vector3 direction, out Raycasthit hits, float distance);
This overloaded method uses a parameter of type Raycasthit, which is the object to which the light is colliding
void Update () {Float DirX = Input.getaxis ("Mouse X"); float DirY = Input.getaxis ("Mouse Y"); Transform. Rotate (DirY,-dirx, 0); Uptateraycasthit ();} void Updateraycasthit () {Raycasthit hit; if (Physics.raycast (Transform.position, Transform.forward, out hit)) {Distroy (Hit.collider.gameObject)}}
This article is from the "Better_power_wisdom" blog, make sure to keep this source http://aonaufly.blog.51cto.com/3554853/1701717
raycasting Ray projection