Use of Physics.Raycast in Unity

Physics.Raycast Introduction ▼

The parameters provided in the official documentation of Physics.Raycast are as follows  

 Physics.Raycast emits a ray from the specified position, returning true if the ray collides with the object, otherwise returning false

The starting point and direction of the ray cannot be omitted, and other parameters can be omitted.

Case▼

  • The 1st and 2nd parameters define the starting point and direction of a ray
  • The third parameter is RaycastHi to obtain parameters from the ray
    RaycastHit hit;
    void Update()
    {
        //如果检测到物体
        if (Physics.Raycast(transform.position, transform.forward, out hit))
        {
             Debug.Log(hit.collider);//命中的 Collider。
            //打印一条从物体到碰撞点的红色射线,hit.point世界空间中射线命中碰撞体的撞击点
            Debug.DrawLine(transform.position, hit.point, Color.red);
        }
        else //没有检测到物体
        {
            //打印一条从物体出发的黄色射线
            Debug.DrawRay(transform.position, transform.position + transform.forward * 100, Color.yellow);
        }
    }

The running effect is as follows. You can see that the ray collides with other objects and turns red ▼ 

You can also directly define a ray to replace 1 or 2 parameters. The code below has the same functionality as the code above

    Ray ray;
    RaycastHit hit;
    void Update()
    {
        ray = new Ray(transform.position, transform.forward);
        //如果检测到物体
        if (Physics.Raycast(ray, out hit))
        {
             Debug.Log(hit.collider);//输出命中的 Collider。
            //打印一条从物体到碰撞点的射线,hit.point世界空间中射线命中碰撞体的撞击点
             Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100,Color.red);
        }
        else //没有检测到物体
        {
            Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100, Color.yellow);
        }
    }

After setting a detection distance for the ray, objects outside the ray distance will not collide.

 Physics.Raycast(ray, out hit,3) (You can see that the ray turns red after hitting the first block, but does not change after hitting the second block , this is because the second block is beyond the ray detection range) ▼

 After setting a mask parameter to Physics.Raycast, you can selectively ignore colliders.

(Physics.Raycast(ray, out hit, 10, mask)

Here we set the cube Layer to Default and the spherical Layer to Sphere. Then select Mask as Sphere

    Ray ray;
    RaycastHit hit;
    public LayerMask mask;
    void Update()
    {
        ray = new Ray(transform.position, transform.forward);
        //如果检测到物体 
        if (Physics.Raycast(ray, out hit, 10, mask))
        {
            Debug.Log(hit.collider);//输出命中的 Collider。
            //打印一条从物体到碰撞点的射线,hit.point世界空间中射线命中碰撞体的撞击点
            Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100,Color.red);
        }
        else //没有检测到物体
        {Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100, Color.yellow);
        }
    }

 The operation effect is as follows. The ray does not change when it touches the cube, but turns red when it touches the sphere▼

After setting a QueryTriggerInteraction.Ignore parameter for Physics.Raycast, the ray no longer detects the trigger

 ▼The operation is as follows▼

Select the Is Triggr property for the red square. The blue square is not selected.

  •  QueryTriggerInteraction.Collide detects triggers
  •  QueryTriggerInteraction.Ignore does not detect triggers
     Ray ray;
    RaycastHit hit;
    public LayerMask mask;
    void Update()
    {
        ray = new Ray(transform.position, transform.forward);
        //如果检测到物体  
        //QueryTriggerInteraction.Collide 检测触发器
        //QueryTriggerInteraction.Ignore 不检测触发器
        if (Physics.Raycast(ray, out hit, 10, mask, QueryTriggerInteraction.Ignore))
        {
            Debug.Log(hit.collider);//输出命中的 Collider。
            //打印一条从物体到碰撞点的射线,hit.point世界空间中射线命中碰撞体的撞击点
            Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100,Color.red);
        }
        else //没有检测到物体
        {
            Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100, Color.yellow);
        }
    }

The running effect is as follows ▼ (The ray does not change when it collides with the red square, and turns red when it collides with the blue square)

Use of Physics.Raycast ▼

  • Cooperate with the pathfinding navigation system to perform the function of moving to the mouse click position.

---Basic use of pathfinding navigation in Unity

        using UnityEngine;
        using UnityEngine.AI;
    
        public class MoveToClickPoint : MonoBehaviour {
            NavMeshAgent agent;
        
            void Start() {
                agent = GetComponent<NavMeshAgent>();
            }
        
            void Update() {
                if (Input.GetMouseButtonDown(0)) {
                    RaycastHit hit;
                
                    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) {
                        agent.destination = hit.point;
                    }
                }
            }
        } 

Guess you like

Origin blog.csdn.net/cxy15978/article/details/130094378