lvjincheng 4 лет назад
Родитель
Сommit
4fb290a30c
1 измененных файлов с 28 добавлено и 24 удалено
  1. 28 24
      Assets/BowArrow/Scripts/Game/Arrow.cs

+ 28 - 24
Assets/BowArrow/Scripts/Game/Arrow.cs

@@ -193,7 +193,7 @@ public class Arrow : MonoBehaviour
     }
 
     void Update() {
-        UpdateFlyWithoutPhysics();
+        UpdateFlyLogic();
     }
 
     void FlyTimeOut() {
@@ -203,16 +203,15 @@ public class Arrow : MonoBehaviour
         nextShoot();
     }
 
-    bool useUpdatePhysics = false;
-    float flyTimeWithoutPhysics = 0;
-    Vector3 finalPoint = default;
-    /**更新箭的飞行轨迹(非Unity物理运算)*/
-    void UpdateFlyWithoutPhysics() {
+    float logicFlyTime = 0;
+    Vector3 finalPoint;
+    /**飞行帧逻辑(运动学) */
+    void UpdateFlyLogic() {
         if (isHit) return;
-        flyTimeWithoutPhysics += Time.deltaTime;
+        logicFlyTime += Time.deltaTime;
         Vector3 destination = finalPoint;
         float vx = Mathf.Cos(parabolaAngleInRadian) * mySpeed;
-        float t = flyTimeWithoutPhysics;
+        float t = logicFlyTime;
         float vy = Mathf.Sin(parabolaAngleInRadian) * mySpeed + Physics.gravity.y * t;
         float dy = Mathf.Sin(parabolaAngleInRadian) * mySpeed * t + 0.5f * Physics.gravity.y * Mathf.Pow(t, 2);
         float dx = vx * t;
@@ -231,23 +230,28 @@ public class Arrow : MonoBehaviour
         RaycastHit raycastHit;
         bool raycastResult = Physics.Raycast(ray, out raycastHit, deltaDistance);
         if (raycastResult) {
-            this.transform.Find("Head").position = raycastHit.point;
-            this.transform.SetParent(raycastHit.transform.parent);
-            string targetName = raycastHit.transform.gameObject.name;
-            if (targetName == "TargetBody") {
-                Vector3 hitPoint = raycastHit.point;
-                if (rayHitTargetBody && canPerfectHit) {
-                    hitPoint = absoluteRay.point;
-                    this.transform.Find("Head").position = hitPoint;
-                }
-                raycastHit.transform.GetComponent<TargetBody>().Hit(this, hitPoint);
-            } else if (raycastHit.transform.GetComponent<TargetOutBound>()) {
-                FlyTimeOut();
-            } else {
-                Hit();
-                GameMgr.ins.gameMode.HitTarget(0);
-                AudioMgr.ins.PlayCheer(false);
+            OnHitAnyInFlyLogic(raycastHit);
+        }
+    }
+
+    //飞行逻辑中检测到碰撞
+    void OnHitAnyInFlyLogic(RaycastHit raycastHit) {
+        this.transform.Find("Head").position = raycastHit.point;
+        this.transform.SetParent(raycastHit.transform.parent);
+        string targetName = raycastHit.transform.gameObject.name;
+        if (targetName == "TargetBody") {
+            Vector3 hitPoint = raycastHit.point;
+            if (rayHitTargetBody && canPerfectHit) {
+                hitPoint = absoluteRay.point;
+                this.transform.Find("Head").position = hitPoint;
             }
+            raycastHit.transform.GetComponent<TargetBody>().Hit(this, hitPoint);
+        } else if (raycastHit.transform.GetComponent<TargetOutBound>()) {
+            FlyTimeOut();
+        } else {
+            Hit();
+            GameMgr.ins.gameMode.HitTarget(0);
+            AudioMgr.ins.PlayCheer(false);
         }
     }