浏览代码

射箭调整抛物线

lvjincheng 4 年之前
父节点
当前提交
33db4504d8
共有 1 个文件被更改,包括 76 次插入17 次删除
  1. 76 17
      Assets/BowArrow/Scripts/Game/Arrow.cs

+ 76 - 17
Assets/BowArrow/Scripts/Game/Arrow.cs

@@ -28,30 +28,33 @@ public class Arrow : MonoBehaviour
             mySpeed *= (1 + GameAssistUI.ins.shootScaleValue);
         } 
 
-        newRigidbody.velocity = this.transform.forward * mySpeed;
-        newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
-
         //预测和判断是否需要抛物线动画
         if (raycastHit.transform.gameObject.name == "TargetBody") {
-            Vector3 targetPosition = raycastHit.transform.position;
-            Vector3 headPosition = this.transform.position; 
-            float moveDistance = Vector3.Distance(targetPosition, headPosition);
-            float canFlyTime = Mathf.Sqrt(headPosition.y * 2 / 9.81f);
-            float estimateTime = moveDistance / mySpeed;
-            // if (estimateTime < canFlyTime) {
-            //     float t = 1.5f;
-            //     float g = 9.81f;
-            //     float vy =  g * t / 2;
-            //     float vx = moveDistance / t;
-            //     newRigidbody.velocity = new Vector3(0, vy, vx);
-            //     mySpeed = newRigidbody.velocity.magnitude;
-            // }
+            // Vector3 targetPosition = raycastHit.transform.position;
+            // Vector3 tailPosition = this.transform.position; 
+            // float moveDistance = Vector3.Distance(targetPosition, tailPosition);
+            // float canFlyTime = Mathf.Sqrt(tailPosition.y * 2 / 9.81f);
+            // float estimateTime = moveDistance / mySpeed;
+            
+            // // if (estimateTime < canFlyTime) {
+            // //     float t = 1.5f;
+            // //     float g = 9.81f;
+            // //     float vy =  g * t / 2;
+            // //     float vx = moveDistance / t;
+            // //     newRigidbody.velocity = new Vector3(0, vy, vx);
+            // //     mySpeed = newRigidbody.velocity.magnitude;
+            // // }
+
+            ApplyParabolaAngle(raycastHit.point);            
             float distance = TargetBody.ins.GetDistance();
             if (Mathf.RoundToInt(distance) >= 50) {
                 if (Random.value < 0.5) simulatedParabola = true;
             }
         }
 
+        newRigidbody.velocity = this.transform.forward * mySpeed;
+        newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
+
         Transform cameraTF = this.transform.Find("Camera");
         cameraTF.gameObject.SetActive(true);
         cameraTF.gameObject.AddComponent<ArrowCamera>().arrow = this;
@@ -59,6 +62,62 @@ public class Arrow : MonoBehaviour
         this.activeEffectTrail(true);
     }
 
+    void ApplyParabolaAngle(Vector3 destination)
+    {
+        float angleX = 0;
+        float distX = Vector2.Distance(
+            new Vector2(destination.x, destination.z), 
+            new Vector2(this.transform.position.x, this.transform.position.z)
+        );
+        float distY = destination.y - this.transform.position.y;
+        float posBase = (Physics.gravity.y * Mathf.Pow(distX, 2.0f)) / (2.0f * Mathf.Pow(mySpeed, 2.0f));
+        float posX = distX / posBase;
+        float posY = (Mathf.Pow(posX, 2.0f) / 4.0f) - ((posBase - distY) / posBase);
+        bool paoshe = false; 
+        if (posY >= 0.0f)
+        {
+            if (paoshe)
+                angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f + Mathf.Pow(posY, 0.5f));
+            else
+                angleX = Mathf.Rad2Deg * Mathf.Atan(-posX / 2.0f - Mathf.Pow(posY, 0.5f));
+        }
+        else
+        {
+            angleX = 45.0f;
+        }
+        Debug.LogWarning(angleX);
+
+        // bool hasBestAngle = false;
+        // for (float angle = -5.0f; angle < 20.0f; angle += 0.3f) {
+        //     float vx = mySpeed * Mathf.Cos(angle / Mathf.Rad2Deg);
+        //     float vy = mySpeed * Mathf.Sin(angle / Mathf.Rad2Deg);
+        //     // vx * t = distX;
+        //     float t = distX / vx;
+        //     // vy * t + 1/2 * Physics.gravity.y * Mathf.Pow(t, 2) = distY;
+        //     float realY = vy * t + 1/2 * Physics.gravity.y * Mathf.Pow(t, 2);
+        //     if (Mathf.Abs(realY - distY) < 0.3f) {
+        //         angleX = angle;
+        //         hasBestAngle = true;
+        //         // Debug.Log("disY" + Mathf.Abs(realY - distY) + " angleX:" + angleX + " realY:" + realY + " distY:" + distY);
+        //         Debug.Log()
+        //         break;
+        //     }
+        // }
+
+        // if (hasBestAngle) {
+        //     // Debug.Log(angleX);
+        // } else {
+        //     angleX = 20;
+        //     Debug.Log("AAA");
+        // }
+        
+
+
+        Vector3 finalAngle = this.transform.eulerAngles;
+        finalAngle = new Vector3(-angleX, finalAngle.y, finalAngle.z);
+        this.transform.localRotation = Quaternion.Euler(finalAngle);
+    }
+
     void OnDestroy() {
         GameMgr.ins.gameMode.ResumeTimeCounting(this);
         if (this.newRigidbody) 
@@ -79,7 +138,7 @@ public class Arrow : MonoBehaviour
             flyTime += Time.deltaTime;
             this.position = this.transform.position;
             this.rotation = this.transform.rotation;
-            if (flyTime > 4) {
+            if (flyTime > 14) {
                 Destroy(gameObject);
                 GameMgr.ins.gameMode.HitTarget(0);
                 AudioMgr.ins.PlayCheer(false);