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()區別

相關文章
相關標籤/搜索