?????個(gè)人主頁:@元宇宙-秩沅
????? hallo 歡迎 點(diǎn)贊?? 收藏? 留言?? 加關(guān)注?!
????? 本文由 秩沅 原創(chuàng)
????? 收錄于專欄:unity每日一記
????推薦文章?
?【Unityc#專題篇】之c#系統(tǒng)化大禮包】
?【unity數(shù)據(jù)持久化】數(shù)據(jù)管理類_PlayerPrfs
?【unity本站最全系列】unity常用API大全一篇文章足以
??
??(A) 碰撞器和觸發(fā)器的網(wǎng)格檢測
??(B) 向量點(diǎn)乘檢測
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項(xiàng)目: ______________
//___________功能: 關(guān)于物體的檢測:物體在前方5米內(nèi)45度角中方可檢測到
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class targetPrage : MonoBehaviour
{
public Transform target; //目標(biāo)物體
private void Update()
{
if(Vector3 .Distance(transform.position ,target.position )<= 5)
{
//向量檢測
//B-A = AB,此時(shí)是單位向量的乘積
//點(diǎn)乘的結(jié)果是余弦值
float vaule = Vector3.Dot(transform.forward, (target.position - transform.position).normalized );
//反三角函數(shù)計(jì)算夾角
if(Mathf.Acos(vaule * Mathf.Deg2Rad)<30f)
{
Debug.Log("敵軍來襲");
}
}
}
}
??(C) Vector3API檢測
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項(xiàng)目: ______________
//___________功能: 關(guān)于物體的檢測:物體在前方5米內(nèi)45度角中方可檢測到
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class targetPrage : MonoBehaviour
{
public Transform target; //目標(biāo)物體
private void Update()
{
if(Vector3 .Distance(transform.position ,target.position )<= 5)
{
//向量檢測
//B-A = AB,此時(shí)是單位向量的乘積
float vaule = Vector3.Dot(transform.forward, (target.position - transform.position).normalized );
if(Vector3 .Angle (transform.position ,target .position )<30f)
{
Debug.Log("敵軍來襲");
}
}
}
}
??(D)物理范圍瞬時(shí)檢測
特點(diǎn):
- 1.執(zhí)行該句代碼時(shí) 進(jìn)行一次范圍檢測 它是瞬時(shí)的
- 2.范圍檢測相關(guān)API 并不會(huì)真正產(chǎn)生一個(gè)碰撞器 只是碰撞判斷計(jì)算而已
共同參數(shù):
- 參數(shù)一:物體中心點(diǎn)
- 參數(shù)二:物體的邊長大小
- 參數(shù)三:物體的角度
- 參數(shù)四:檢測指定層級(jí)(不填檢測所有層)
- 參數(shù)五:是否忽略觸發(fā)器 UseGlobal-使用全局設(shè)置 Collide-檢測觸發(fā)器 Ignore-忽略觸發(fā)器 (不填使用UseGlobal)
- 返回值:在該范圍內(nèi)的觸發(fā)器(得到了對(duì)象觸發(fā)器就可以得到對(duì)象的所有信息)
1.方塊狀范圍檢測
- Physics.OverlapBox ——返回值為數(shù)組,存儲(chǔ)檢測到的碰撞器
Collider[] colliders = Physics.OverlapBox( Vector3.zero, Vector3.one,
Quaternion.AngleAxis(45, Vector3.up),
1 << LayerMask.NameToLayer("UI") |
1 << LayerMask.NameToLayer("Default"), QueryTriggerInteraction.UseGlobal);
- Physics.OverlapBoxNonAlloc——返回值為Int 表示檢測的數(shù)量(最多6個(gè)參數(shù))
if(Physics.OverlapBoxNonAlloc(Vector3.zero, Vector3.one, 自定義數(shù)組名) != 0)
2.球形狀范圍檢測
無角度參數(shù)
參數(shù)二為球半徑
- Physics.OverlapSphere
colliders = Physics.OverlapSphere(Vector3.zero, 5, 1 << LayerMask.NameToLayer("Default"));
- Physics.OverlapSphereNonAlloc——同BOX
if( Physics.OverlapSphereNonAlloc(Vector3.zero, 5, colliders) != 0 )
3.膠囊體范圍檢測
參數(shù)一:半圓一中心點(diǎn)
參數(shù)二:半圓二中心點(diǎn)
參數(shù)三:半圓半徑
- Physics.OverlapCapsule
colliders = Physics.OverlapCapsule(Vector3.zero, Vector3.up, 1, 1 << LayerMask.NameToLayer("UI"), QueryTriggerInteraction.UseGlobal);
- Physics.OverlapCapsuleNonAlloc
if ( Physics.OverlapCapsuleNonAlloc(Vector3.zero, Vector3.up, 1, colliders ) != 0 )
??(O)Ray射線檢測
-
特點(diǎn)
只需要判斷一條線和物體的碰撞情況
可以在指定點(diǎn)發(fā)射一個(gè)指定方向的射線
判斷該射線與哪些碰撞器相交,得到對(duì)應(yīng)對(duì)象
瞬時(shí) -
應(yīng)用場景
1.鼠標(biāo)選擇場景上一物體
2.FPS射擊游戲(無彈道-不產(chǎn)生實(shí)際的子彈對(duì)象進(jìn)行移動(dòng))等
射線聲明
API
- Ray X = new Ray(Vector3.right, Vector3.forward);
參數(shù)一 | 參數(shù)二 |
---|---|
起點(diǎn) | 方向 |
X.origin | X.direction |
-
Ray XX = Camera.main.ScreenPointToRay(Input.mousePosition);
屏幕視口坐標(biāo)轉(zhuǎn)成射線——鼠標(biāo)點(diǎn)擊的地方變成射線
Physics.Raycast 普通射線
- Physics.Raycast 無法檢測碰到了誰,只會(huì)檢測碰到了沒有
最多有16個(gè)重載
Physics.Raycast常用參數(shù) | 作用 |
---|---|
參數(shù)一 | 射線 |
參數(shù)二 | 檢測的最大距離 超出這個(gè)距離不檢測 |
參數(shù)三 | 檢測指定層級(jí)(不填檢測所有層) |
參數(shù)四 | 是否忽略觸發(fā)器 UseGlobal-使用全局設(shè)置 Collide-檢測觸發(fā)器 Ignore-忽略觸發(fā)器 不填使用UseGlobal |
返回值 | bool 當(dāng)碰撞到對(duì)象時(shí) 返回 true 沒有 返回false |
//第一種寫法
Physics.Raycast(XX, 1000,
1 << LayerMask.NameToLayer("層級(jí)名字"),
QueryTriggerInteraction.UseGlobal )
//第二種寫法
Physics.Raycast(Vector3.right, Vector3.forward,
1 << LayerMask.NameToLayer("層級(jí)名字"),
QueryTriggerInteraction.UseGlobal )
RaycastHit 物體信息類
-
RaycastHit 物體信息類——得到相交的單個(gè)物體物理信息
RaycastHit 在Physics.Raycast的應(yīng)用 | 作用 |
---|---|
參數(shù)一 | 射線 |
參數(shù)二 | out RaycastHit 為什么是out ?RaycastHit是結(jié)構(gòu)體 是值類型 out加上去就變成了引用類型,而RaycastHit沒有復(fù)制所以不用ref |
參數(shù)三 | 檢測的最大距離 超出這個(gè)距離不檢測 |
參數(shù)四 | 檢測指定層級(jí)(不填檢測所有層) |
參數(shù)五 | 是否忽略觸發(fā)器 UseGlobal-使用全局設(shè)置 Collide-檢測觸發(fā)器 Ignore-忽略觸發(fā)器 不填使用UseGlobal |
返回值 | bool 當(dāng)碰撞到對(duì)象時(shí) 返回 true 沒有 返回false |
//寫法一
RaycastHit YY;
if( Physics.Raycast(XX, out YY, 1000,
1<<LayerMask.NameToLayer("層級(jí)名字"),
QueryTriggerInteraction.UseGlobal) )
//寫法二
if( Physics.Raycast(Vector3.right, Vector3.forward, out YY, 1000,
1<<LayerMask.NameToLayer("層級(jí)名字"),
QueryTriggerInteraction.UseGlobal) )
- 碰撞到物體的名字 YY.collider.gameObject.name;
- 碰撞到的點(diǎn) YY.point
- 法線信息 YY.normal
- 碰撞到對(duì)象的位置 YY.transform.position
- 碰撞到對(duì)象 離自己的距離 YY.distance等等
Physics.RaycastAll 得到多個(gè)物體信息
- RaycastHit[] XX= Physics.RaycastAll——得到相交的多個(gè)物體物理信息
特點(diǎn): 先碰到的在數(shù)組的后面
Physics.RaycastNonAlloc 返回的碰撞的數(shù)量
- Physics.RaycastNonAlloc——返回的碰撞的數(shù)量 通過out得到數(shù)據(jù)
if((r3, XX, 1000, 1 << LayerMask.NameToLayer("Monster"),
QueryTriggerInteraction.UseGlobal) > 0 )
{
}
????系統(tǒng)路線學(xué)習(xí)點(diǎn)擊跳轉(zhuǎn)?
?【Unityc#專題篇】之c#進(jìn)階篇】
?【Unityc#專題篇】之c#核心篇】
?【Unityc#專題篇】之c#基礎(chǔ)篇】
?【Unity-c#專題篇】之c#入門篇】
?【Unityc#專題篇】—進(jìn)階章題單實(shí)踐練習(xí)
?【Unityc#專題篇】—基礎(chǔ)章題單實(shí)踐練習(xí)
?【Unityc#專題篇】—核心章題單實(shí)踐練習(xí)
你們的點(diǎn)贊?? 收藏? 留言?? 關(guān)注?是我持續(xù)創(chuàng)作,輸出優(yōu)質(zhì)內(nèi)容的最大動(dòng)力!、文章來源:http://www.zghlxwxcb.cn/news/detail-648686.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-648686.html
到了這里,關(guān)于【Unity每日一記】關(guān)于物體(敵方)檢測—(向量點(diǎn)乘相關(guān))的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!