Unity3D_(API)射线检测Raycast()

 

 

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

 

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

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

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

  4、指定检测碰撞Tag层spa

 

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

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

   

 

  建立一个Cube做为地面,重命名为Groundorm

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

  建立三个Cube做为目标,用来判断射线是否检测到目标blog

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

 

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

 

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);

 

 

2、检测前方是否有游戏物体(射线长度为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);

 

 

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

 

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);

 

 

4、指定检测碰撞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()区别

相关文章
相关标签/搜索