国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

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

分享到微博請遵守國家法律
广元市| 航空| 河南省| 久治县| 兴安盟| 安康市| 方山县| 安福县| 富源县| 新兴县| 扶沟县| 宁化县| 武安市| 伊金霍洛旗| 绥阳县| 栾城县| 呼玛县| 达日县| 江都市| 晋州市| 丽水市| 专栏| 临漳县| 集贤县| 阿克陶县| 博客| 庆城县| 达孜县| 岑溪市| 闸北区| 江孜县| 东兰县| 旅游| 台湾省| 荔波县| 江西省| 长宁区| 千阳县| 防城港市| 远安县| 濉溪县|