五月天青色头像情侣网名,国产亚洲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小白:(思路向)做子彈穿透檢測的的評論 (共 條)

分享到微博請遵守國家法律
上栗县| 恩施市| 石棉县| 贡觉县| 三台县| 定远县| 玉林市| 霍州市| 萝北县| 孟津县| 内丘县| 淮北市| 广宁县| 高陵县| 苍溪县| 宣恩县| 大庆市| 新绛县| 那曲县| 广宁县| 榆中县| 桦南县| 闽侯县| 明光市| 绥芬河市| 松潘县| 乐安县| 枝江市| 扎鲁特旗| 舟曲县| 汉川市| 秦皇岛市| 广南县| 姜堰市| 阿拉善左旗| 平原县| 屏东市| 张家界市| 平陆县| 沁源县| 周宁县|