• Unity3D_(API)射线检测Raycast()


      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()区别

    (如需转载学习,请标明出处)
  • 相关阅读:
    SQL SERVER使用技巧集
    WIN32串口编程
    经典FLASH收藏
    Windows下WinsockAPI研究
    数据库连接大全[转自中国站长网]
    VirtualBox自动重启之谜
    写个设置命令的VBS脚本工具。
    VB中KeyCode的取法
    实现串口编程的三种方法
    .NET的命名空间
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/9941156.html
Copyright © 2020-2023  润新知