1138720556Gary

 

 

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

 

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

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

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

  四、指定检测碰撞Tag层

 

 

  创建一个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);

 

相关文章:

  • 2022-12-23
  • 2022-01-12
  • 2022-12-23
  • 2021-12-03
  • 2021-07-14
  • 2022-02-06
  • 2021-05-26
猜你喜欢
  • 2021-11-10
  • 2021-10-05
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2019-10-24
相关资源
相似解决方案