Unity的射线
Ray射線
今天我要總結(jié)的使Unity有關(guān)射線的相關(guān)東西,如有某些地方理解錯誤還望指正,謝謝!
1.什么Ray射線
首先,我們要知道射線構(gòu)成的兩大要素:起點和方向,所以實例化一條射線需要這兩個參數(shù)。
射線的實例化:
(關(guān)鍵字)Ray 變量名 = new (關(guān)鍵字)Ray(Vector3 origin,Vector3 direction);
第一個參數(shù)origin表示射線的起點,第二個參數(shù)direction表示射線的方向。
特殊的Camera.main.ScreenPointToRay(Vector3 targetPosition)
表示攝像機向目標位置發(fā)射射線其中targetPosition通常會使用屏幕坐標
Input.mousePosition,表示攝像機向鼠標位置發(fā)射射線。
2. RaycastHit類的介紹
作用:用于儲存發(fā)射射線產(chǎn)生的碰撞信息。
常用的成員變量有:
point:射線與碰撞器的交點坐標
collider:與射線發(fā)生碰撞的物體的碰撞器
distance:射線起點到與碰撞物體交點的坐標
3.射線的調(diào)試(顯示)
Debug.DrawRay(Vector3 start, Vector3 dir, Color color, float duration)
參數(shù)start:射線的起點
參數(shù)dir:射線的方向
參數(shù)color:調(diào)試中射線的顏色
參數(shù)duration:調(diào)試中射線的存在的時間
當然Debug.DrawRay函數(shù)有很多重載,比如我們可以不寫Color color, float duration,那么我們的射線在編輯模式下,射線的顏色就默認是白色,存在時間默認是一幀;
把下面這個腳本放在一個物體,然后運行,在編輯模式就可看到以(0,0,0)為起點,方向為沿X軸正方向的紅色 射線。
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Shoot : MonoBehaviour {void Update(){Vector3 origin = new Vector3(0, 0, 0);Vector3 direction = new Vector3(1f, 0, 0);Ray ray = new Ray(origin, direction);Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 1f);} }
4.射線的發(fā)射:bool Physics.Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask,QueryTriggerInteraction );
參數(shù)start:射線的起點
參數(shù)ray:射線
參數(shù)hitInfo:儲存被射線碰撞到的物體信息
參數(shù)maxDistance:射線的最大距離
參數(shù)layerMask:射線檢測的圖層,用int 十進制轉(zhuǎn)化為二進制判斷,(十進制)2->(二進制)10,表示 檢 測第一層TransparentFX;
參數(shù)QueryTriggerInteraction :查詢是否忽略is Trigger(觸發(fā)器),忽略使用QueryTriggerInteraction.Ignore,檢測使用QueryTriggerInteraction.Collide
函數(shù)描述:發(fā)射一條射線ray,這條射線的長度為maxDistance,如果這條射線在指定圖層碰到了其它帶有碰撞器的物體,則把該物體信息儲存在hitInfo中,且返回true,如果沒碰到物體就返回false;
當然這個函數(shù)還有其它重載,比如我們可以不寫 float maxDistance, int layerMask,QueryTriggerInteraction 這三個參數(shù),那么發(fā)射射線的最大距離默認為無限大,圖層就默認檢測除了2 圖層Ignore Raycast外的其它圖層,參數(shù)QueryTriggerInteraction 就為QueryTriggerInteraction.UseGlobal,也就是我們項目設置里面的默認設置。如果勾了就默認檢測,否則就忽略;
然后我們在一個物體上添加如下腳本,并在射線經(jīng)過的地方擺一個盒形碰撞器。然后運行并點擊一下鼠標左鍵,然后你就會在控制臺上發(fā)現(xiàn)射線碰撞到的物體的碰撞器的種類。
射線的應用場景
射線的應用場景有很多,由于篇幅有限,我們就模擬一下人物射擊。
首先我們需要在場景中創(chuàng)建一個球形碰撞器,然后通過拖動把它變成預制體,然后刪除場景中的球形碰撞器,然后在在預制體上增加以下腳本。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class bullet : MonoBehaviour {Vector3 shootdirection;float speed;// Start is called before the first frame updatevoid Start(){speed = 6f;}// Update is called once per framevoid Update(){transform.Translate(shootdirection*speed*Time.deltaTime,Space.World);}/// <summary>/// 初始化子彈移動的方向/// </summary>/// <param name="shoot"></param>public void startState(Ray shoot){ shootdirection = shoot.direction;} }然后我們在場景中再創(chuàng)鍵一個球形碰撞器模擬人物,并在上面添加如下腳本。
using System.Collections; using System.Collections.Generic; using UnityEngine;public class roleshoot : MonoBehaviour {bool isnoShoot;public bullet bullets;// Update is called once per framevoid Update(){isnoShoot = Input.GetMouseButtonDown(0);if(isnoShoot){RoleShoot();}}void RoleShoot(){//攝像機向鼠標所在的地方發(fā)射射線Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;//定義一個RaycastHit接受被碰撞物體的信息//發(fā)射射線Physics.Raycast(ray, out hit, 100f, 1, QueryTriggerInteraction.Collide);Vector3 shootdirection = hit.point - transform.position;//實例主角應該發(fā)射的射線Ray roleray = new Ray(transform.position, shootdirection);//讓射線在編輯模式中可見Debug.DrawRay(roleray.origin, roleray.direction*1000f, Color.red, 2f);//生成子彈,bullet bullet = Instantiate(bullets, transform.position, bullets.gameObject.transform.rotation);//初始化子彈方向bullet.startState(roleray);} }然后手動添加預制體.
然后點擊地面就可以發(fā)射子彈了。
總結(jié)
- 上一篇: unity射线使用方法详解
- 下一篇: OPENGL 射线拾取法