无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(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小白:(思路向)做子彈穿透檢測的的評論 (共 條)

分享到微博請遵守國家法律
扎鲁特旗| 东明县| 宁海县| 海门市| 治县。| 柳州市| 鹤峰县| 二连浩特市| 韩城市| 嘉荫县| 威远县| 河间市| 福泉市| 武安市| 遂宁市| 福海县| 盐山县| 鄂托克旗| 南溪县| 荣昌县| 南宫市| 神农架林区| 工布江达县| 泗洪县| 罗城| 榆树市| 阿鲁科尔沁旗| 浠水县| 宁南县| 平远县| 南和县| 云浮市| 内乡县| 玉田县| 万山特区| 阿勒泰市| 福建省| 九龙城区| 手机| 广饶县| 白朗县|