射线

射线

1.射线检测

1.1检测单个物体

using UnityEngine;

public class Juse : MonoBehaviour
{
    public LayerMask layerMask;
    private void Start()
    {
        layerMask = LayerMask.GetMask("ine");
    }
    private void Update()
    {
        //通过起始点和方向定义一条射线
        Ray ray = new Ray(transform.position, transform.forward);
        //射线碰撞信息
        RaycastHit hit;
        //打印射线第一个检测到的层级设置成“ine”的物体的名称,射线长度为100。
        if (Physics.Raycast(ray,out hit,100f,layerMask))
        {
            Debug.Log("hitname:" + hit.transform.name);
        }
    }
}

1.2检测多个物体

using UnityEngine;

public class Juse : MonoBehaviour
{
    private void Update()
    {
        RaycastHit[] hits = Physics.RaycastAll(transform.position, transform.forward, 100f);
        for(int i = 0; i < hits.Length; i++)
        {
            Debug.Log("NO."+i+":"+ hits[i].collider.name);
        }
    }
}

1.3球型检测

using UnityEngine;

public class Juse : MonoBehaviour
{
    private void Update()
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, 10f);
        for (int i = 0; i < colliders.Length; i++)
        {
            Debug.Log("NO."+i+":"+ colliders[i].gameObject.name);
        }
    }
}

1.4常用检测

using UnityEngine;

public class Juse : MonoBehaviour
{
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Input.GetMouseButton(0))
        {
            if (Physics.Raycast(ray, out hit, 100f, 1 << LayerMask.NameToLayer("Water")))//只检测层级名称为“Water”的物体
            {
                Debug.Log("hit:" + hit.transform.name);
            }
        }
    }
}

  

using UnityEngine;

public class Juse : MonoBehaviour
{
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Input.GetMouseButton(0))
        {
            if (Physics.Raycast(ray, out hit, 100f, 1 << 10))//只检测层级为10的物体
            {
                Debug.Log("hit:" + hit.transform.name);
            }
        }
    }
}

  

using UnityEngine;

public class Juse : MonoBehaviour
{
    private LayerMask layerMask;//也可设置成public,在面板中设定
    void Start()
    {
        layerMask = 1 << 1; //表示允许检测第一层Layer。
        layerMask += 1 << 2; //增加允许检测第二层Layer。
        layerMask |= 1 << 3; //增加允许检测第三层Layer。
        layerMask = ~layerMask;//取反
    }
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Input.GetMouseButton(0))
        {
            if (Physics.Raycast(ray, out hit, 100f, layerMask))//检测除1,2,3层级外所有层的物体
            {
                Debug.Log("hit:" + hit.transform.name);
            }
        }
    }
}

  

2.射线绘制

绘制射线一般用于调试,有两种方式

2.1方式一

using UnityEngine;

public class Juse : MonoBehaviour
{
    private void Start()
    {
        //两个点确定的射线。参数分别为:发射点位置,另一个点的位置,射线颜色,持续时间,能否被遮挡
        Debug.DrawLine(transform.position, new Vector3(0, 0, 10), Color.green, 10f, true);
    }
}

2.2方式二

using UnityEngine;

public class Juse : MonoBehaviour
{
    private void Start()
    {
        //点和方向确定的射线。参数分别为:发射点位置,方向,射线颜色,持续时间,能否被遮挡
        Debug.DrawRay(transform.position, new Vector3(0, 0, 10), Color.green, 10f, true);
    }
}

  

 

  

 

posted @ 2023-03-18 14:24  gatran  阅读(71)  评论(0编辑  收藏  举报