五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

UNITY小白:(思路向)做子彈穿透檢測的

2022-04-04 21:08 作者:熔炎矢紫  | 我要投稿

? ? (本文提供只一種思路,沒有頭緒的同學(xué)可以借鑒)

? ? ?子彈穿透,在很多游戲中都能見到。如武裝突襲、CSGO等


這里我們不討論VAC經(jīng)歷,只來研究子彈穿墻的原理。


如圖,我們要求出紅色線段的長度。但只有一條直線還不夠,因此我們需要在“射線”末端再多加一條直線,反射回射線的起點。

如此,我們只需求那兩兩對應(yīng)的穿透點組就行了。

代碼如下

? ? public Transform startPoint;

? ? public Transform endPoint;

? ? public LayerMask castLayer;

? ? float standardDistance;

? ? RaycastHit[] Hits;

? ? RaycastHit[] ReHits;

? ? bool haveHit;

? ? // Start is called before the first frame update

? ? void Start()

? ? {

? ? ? ? standardDistance = Vector3.Distance(startPoint.position, endPoint.position);

? ? }


? ? // Update is called once per frame

? ? void Update()

? ? {

? ? ? ? Hits = Physics.RaycastAll(startPoint.position, endPoint.position - startPoint.position, standardDistance, castLayer);

? ? ? ? ReHits = Physics.RaycastAll(endPoint.position, startPoint.position - endPoint.position, standardDistance, castLayer);

? ? ? ? haveHit = (Hits.Length > 0);

? ? }


? ? void HaveSameCollider(Collider hitCollider, RaycastHit[] reRaycastHits, out bool haveSameCollider, out Vector3 ReHitPoint)

? ? {

? ? ? ? foreach (var hit in reRaycastHits)

? ? ? ? {

? ? ? ? ? ? if (hitCollider == hit.collider)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? haveSameCollider = true;

? ? ? ? ? ? ? ? ReHitPoint = hit.point;

? ? ? ? ? ? ? ? return;? ? ? ? ? ? ? ? ? ? ? ? ? ? //——————非常重要。此return能終止函數(shù),不然foreach就會一直繼續(xù)下去,最終結(jié)果只能都會是false;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? haveSameCollider = false;

? ? ? ? ReHitPoint = Vector3.zero;

? ? }? ? //此方法用于將hits中的hit一個個拎出來,將其的collider與目標(biāo)集合對比,以達(dá)到尋找相同碰撞體的效果




? ? private void OnDrawGizmos()

? ? {

? ? ? ? if(haveHit)

? ? ? ? {

? ? ? ? ? ? Gizmos.color = Color.green;

? ? ? ? ? ? Gizmos.DrawLine(startPoint.position, endPoint.position);

? ? ? ? ? ? foreach (var hit in Hits)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? HaveSameCollider(hit.collider, ReHits, out bool haveSameCollider, out Vector3 RehitPoint);

? ? ? ? ? ? ? ? if (haveSameCollider)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? Gizmos.color = Color.red;

? ? ? ? ? ? ? ? ? ? Gizmos.DrawSphere(hit.point, 0.05f);

? ? ? ? ? ? ? ? ? ? Gizmos.color = Color.blue;

? ? ? ? ? ? ? ? ? ? Gizmos.DrawSphere(RehitPoint, 0.05f);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? }


結(jié)果如圖:

當(dāng)然,你們也可以在這個基礎(chǔ)上自行添加想要的功能。



UNITY小白:(思路向)做子彈穿透檢測的的評論 (共 條)

分享到微博請遵守國家法律
中方县| 大新县| 武隆县| 万年县| 平山县| 巴林左旗| 平利县| 淅川县| 大姚县| 刚察县| 资中县| 修文县| 淳化县| 昆明市| 花垣县| 通许县| 泗洪县| 民乐县| 鹤庆县| 桦甸市| 手游| 湾仔区| 德令哈市| 临潭县| 苍溪县| 正阳县| 林甸县| 西藏| 丹巴县| 内黄县| 林西县| 大理市| 鹿泉市| 彰武县| 内江市| 离岛区| 突泉县| 冷水江市| 南华县| 贡觉县| 台州市|