zoukankan      html  css  js  c++  java
  • 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()区别

    (如需转载学习,请标明出处)
  • 相关阅读:
    Linux硬盘分区方案
    mysql笔记四:索引查询及处理
    thread 学习笔记
    mysql笔记二:基本数据库、表查询操作
    linux 自学系列:监测端口占用情况
    linux 自学系列:命令行传输文件
    mysql笔记三:基本数据库、表创建更新操作
    mysql笔记五:权限管理
    threading源代码问题,为什么要将引入的变量del?
    linux 自学系列:更改系统语言编码
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/9941156.html
Copyright © 2011-2022 走看看