Unity3D_(API)射线检测

Posted Cynical丶Gary

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D_(API)射线检测相关的知识,希望对你有一定的参考价值。

 

 

  Unity射线检测官方文档:  传送门

 

  一、检测前方是否有游戏物体(射线无限长度)

  二、检测前方是否有游戏物体(射线长度为1m)

  三、检测前方游戏物体碰撞信息(射线无限长度):

  四、指定检测碰撞Tag层

 

  2D射线检测:使用Physics2D.Raycast()

  Raycast()和RaycastAll()区别:Raycast()只检测当前游戏物体,RaycastAll()检测前方所有游戏物体(返回一个数组)

   

 

  创建一个Cube作为地面,重命名为Ground

  创建一个Cube作为游戏玩家(重命名为Player),游戏玩家下再新建一个Cube(重命名为Ray)作为射线发射起点

  创建三个Cube作为目标,用来判断射线是否检测到目标

  给Ray添加射线检测Player.cs脚本

 

一、检测前方是否有游戏物体(射线无限长度):

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        //射线发射的起始位置和方向
        Ray ray = new Ray(transform.position+transform.forward, transform.forward);

        //Raycast()方法判断射线是否被检测
        bool isCollider = Physics.Raycast(ray);

        //输出检测信息,只能为True或False
        Debug.Log(isCollider);
    }
}
Player.cs

 

        //射线发射的起始位置和方向
        Ray ray = new Ray(transform.position+transform.forward, transform.forward);

        //Raycast()方法判断射线是否被检测
        bool isCollider = Physics.Raycast(ray);

        //输出检测信息,只能为True或False
        Debug.Log(isCollider);

 

 

二、检测前方是否有游戏物体(射线长度为1m):

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        //射线发射的起始位置和方向
        Ray ray = new Ray(transform.position+transform.forward, transform.forward);

        //Raycast()方法判断射线是否被检测
        bool isCollider = Physics.Raycast(ray,1);

        //输出检测信息,只能为True或False
        Debug.Log(isCollider);
    }
}
Player.cs

 

     //射线发射的起始位置和方向
        Ray ray = new Ray(transform.position+transform.forward, transform.forward);

        //Raycast()方法判断射线是否被检测
        bool isCollider = Physics.Raycast(ray,1);

        //输出检测信息,只能为True或False
        Debug.Log(isCollider);

 

 

三、检测前方游戏物体碰撞信息(射线无限长度):

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        //射线发射的起始位置和方向
        Ray ray = new Ray(transform.position+transform.forward, transform.forward);

        RaycastHit hit;
        bool isCollider = Physics.Raycast(ray,out hit);

        //输出是否碰撞到物体
        Debug.Log(isCollider);
        //输出碰撞物体名字
        Debug.Log(hit.collider);
        //输出碰撞到物体位置
        Debug.Log(hit.point);
    }
}
Player.cs

 

    //射线发射的起始位置和方向
        Ray ray = new Ray(transform.position+transform.forward, transform.forward);

        RaycastHit hit;
        bool isCollider = Physics.Raycast(ray,out hit);

        //输出是否碰撞到物体
        Debug.Log(isCollider);
        //输出碰撞物体名字
        Debug.Log(hit.collider);
        //输出碰撞到物体位置
        Debug.Log(hit.point);

 

 

四、指定检测碰撞Tag层

  给Cube-1 添加 Gary1标签,其它Cube不做改动

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        //射线发射的起始位置和方向
        Ray ray = new Ray(transform.position+transform.forward, transform.forward);

        //需要进行碰撞的Layout层
        bool isCollider = Physics.Raycast(ray,Mathf.Infinity,LayerMask.GetMask("Gary1"));

        //输出是否碰撞到物体
        Debug.Log(isCollider);
    }
}
Player.cs

 

        //射线发射的起始位置和方向
        Ray ray = new Ray(transform.position+transform.forward, transform.forward);

        //需要进行碰撞的Layout层
        bool isCollider = Physics.Raycast(ray,Mathf.Infinity,LayerMask.GetMask("Gary1"));

        //输出是否碰撞到物体
        Debug.Log(isCollider);

 

Raycast()和RaycastAll()区别

以上是关于Unity3D_(API)射线检测的主要内容,如果未能解决你的问题,请参考以下文章

unity3d 射线的原理,基础用法

unity3d之射线检测

Unity3D日常开发从物体当前方发射一条射线,并返回射线检测到的物体信息

Unity3D日常开发从物体当前方发射一条射线,并返回射线检测到的物体信息

unity3d射线是怎样检测ui的

关于Unity3D中鼠标移动指定物体的解决方案