瀏覽代碼

弓箭测试demo

lvjincheng 4 年之前
父節點
當前提交
c94da120c7

文件差異過大導致無法顯示
+ 188 - 396
Assets/BowArrow/Scenes/Game.unity


+ 2 - 6
Assets/BowArrow/Scripts/Debug/GameDebug.cs

@@ -96,15 +96,11 @@ public class GameDebug : MonoBehaviour
         return angle;
     }
 
-    public void ShowRes(Vector3 basePoint, Vector3 hitPoint, Vector3 offsetAngle) {
-        basePoint /= 0.7f;
-        hitPoint /= 0.7f;
-        float dis = (hitPoint - basePoint).magnitude / 0.7f;
+    public void ShowRes(Vector3 basePoint, Vector3 hitPoint) {
         string text = 
             "准心瞄点 " + V3ToString(basePoint) + "\n" +
             "实际落点 " + V3ToString(hitPoint) + "\n" +
-            "偏移角度 " + V3ToString(offsetAngle) + "\n" +
-            "偏移距离 " + dis.ToString("#0.000") + "m";
+            "偏移距离 " + (hitPoint - basePoint).magnitude.ToString("#0.0000") + "m";
         Log(text);
     }
 

+ 2 - 18
Assets/BowArrow/Scripts/Game/ArmBow.cs

@@ -153,28 +153,13 @@ public class ArmBow : MonoBehaviour
             }
         }
 
-        //debug--20210916-s
-        float randomRadian = Random.value * Mathf.PI * 2;
-        Vector3 randomV3 = new Vector3(-Mathf.Sin(randomRadian) / Mathf.PI * 180f, Mathf.Cos(randomRadian) / Mathf.PI * 180f, 0).normalized;
-        randomV3 *= GameDebug.ins.GetOffsetAngle();
-        //debug--20210916-e
-
-        //射线分别获取绝对角和最终角的对应落点
         Quaternion oldCameraRotation = Camera.main.transform.rotation;
         Camera.main.transform.rotation = absolute_rotation;
         RaycastHit absoluteRay = CrossHair.ins.GetRaycastHit();
-
-        //debug--20210916-s
-        Camera.main.transform.Rotate(randomV3);
-        final_rotation = this.bowCamera.transform.rotation;
-        //debug--20210916-e
-
-        Camera.main.transform.rotation = final_rotation;
-        RaycastHit finalRay = CrossHair.ins.GetRaycastHit();
         Camera.main.transform.rotation = oldCameraRotation;
         
         Vector3 shootOutPosition = this.bowCamera.transform.position;
-        GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, final_rotation);
+        GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, absolute_rotation);
 
         Vector3 s1 = arrowCopy.transform.localScale;
         Vector3 s2 = bowCamera.transform.localScale;
@@ -184,8 +169,7 @@ public class ArmBow : MonoBehaviour
         arrowComp.armBow = this;
         arrowComp.shootOutPosition = shootOutPosition;
         arrowComp.absoluteRay = absoluteRay;
-        arrowComp.finalRay = finalRay;
-        arrowComp.offsetAngle = randomV3;
+        arrowComp.offsetAngle = GameDebug.ins.GetOffsetAngle();
 
         if (ShootCheck.ins && !GameMgr.debugInEditor) 
         {

+ 23 - 20
Assets/BowArrow/Scripts/Game/Arrow.cs

@@ -9,8 +9,7 @@ public class Arrow : MonoBehaviour
     public float flyTime = 0;
     public bool isHit = false;
     public RaycastHit absoluteRay;
-    public RaycastHit finalRay;
-    public Vector3 offsetAngle;
+    public float offsetAngle;
     private float mySpeed = 0;
     public bool simulatedParabola = false; //模拟抛物线 
     public ArmBow armBow;
@@ -37,17 +36,7 @@ public class Arrow : MonoBehaviour
             redCircle.transform.position = -redCircle.transform.forward * 0.001f + absoluteRay.point;
         }
         if (absoluteRay.transform && absoluteRay.transform.gameObject.name == "TargetBody") {  
-            CalculateParabolaAngle(absoluteRay.point); 
-            bool hasAbsoluteAngleX = hasParabolaAngle; 
-            float absoluteAngleX = parabolaAngleInRadian / Mathf.PI * 180;  
-            bool hasFinalAngleX = false;
-            float finalAngleX = 0;
-            if (finalRay.transform && finalRay.transform.gameObject.name == "TargetBody") {
-                CalculateParabolaAngle(finalRay.point);   
-                hasFinalAngleX = hasParabolaAngle; 
-                finalAngleX = parabolaAngleInRadian / Mathf.PI * 180;
-            }
-            SetUpBeforFly(hasAbsoluteAngleX, absoluteAngleX, hasFinalAngleX, finalAngleX);
+            SetUpBeforFly();
         }
 
         newRigidbody.velocity = this.transform.forward * mySpeed;
@@ -68,18 +57,31 @@ public class Arrow : MonoBehaviour
         }
     }
 
-    void SetUpBeforFly(bool hasAbsoluteAngleX, float absoluteAngleX, bool hasFinalAngleX, float finalAngleX)
+    void SetUpBeforFly()
     {
+        CalculateParabolaAngle(absoluteRay.point); 
+
         float maxAngleX = 5;
-        if (!hasAbsoluteAngleX) return;
+        if (!hasParabolaAngle) return;
+        float absoluteAngleX = parabolaAngleInRadian / Mathf.PI * 180;
         if (absoluteAngleX > maxAngleX) return;
-        if (hasFinalAngleX) {
+
+        float finalAngleX = absoluteAngleX + this.offsetAngle;
+        float vy = Mathf.Sin(finalAngleX / 180 * Mathf.PI) * this.mySpeed;
+        float vx = Mathf.Cos(finalAngleX / 180 * Mathf.PI) * this.mySpeed;
+        float deltaX = Mathf.Sqrt(Mathf.Pow(absoluteRay.point.x, 2) + Mathf.Pow(absoluteRay.point.z, 2));
+        float tx = deltaX / vx;
+        float dy = vy * tx + 0.5f * Physics.gravity.y * tx * tx;
+        Debug.Log(dy);
+        if (Mathf.Abs(dy) < 0.62f) {
+            finalPoint = absoluteRay.point;
+            finalPoint.y += dy;
             useUpdatePhysics = true;
             RandomUseSideCamera();
         } else {
             Vector3 eulerAngle = this.transform.eulerAngles;
-            eulerAngle.x = -absoluteAngleX;
-            this.transform.eulerAngles = eulerAngle + offsetAngle;
+            eulerAngle.x = -finalAngleX;
+            this.transform.eulerAngles = eulerAngle;
         }
     }
 
@@ -148,10 +150,11 @@ public class Arrow : MonoBehaviour
 
     bool useUpdatePhysics = false;
     float flyTimeWithoutPhysics = 0;
+    Vector3 finalPoint = default;
     void UpdateFlyWithoutPhysics() {
         if (!useUpdatePhysics || isHit) return;
         flyTimeWithoutPhysics += Time.deltaTime;
-        Vector3 destination = finalRay.point;
+        Vector3 destination = finalPoint;
         float deltaX = Mathf.Sqrt(
             Mathf.Pow(destination.x - shootOutPosition.x, 2) +
             Mathf.Pow(destination.z - shootOutPosition.z, 2)
@@ -180,7 +183,7 @@ public class Arrow : MonoBehaviour
     }
 
     public void Hit() {
-        GameDebug.ins.ShowRes(absoluteRay.point, this.getHeadPosition(), offsetAngle);
+        GameDebug.ins.ShowRes(absoluteRay.point, this.getHeadPosition());
         gameObject.GetComponent<BoxCollider>().enabled = false;
         if (newRigidbody) {
             newRigidbody.useGravity = false;

+ 1 - 1
Assets/BowArrow/Scripts/Game/ArrowCamera.cs

@@ -38,7 +38,7 @@ class ArrowCameraTemplate2 : ArrowCameraTemplate
     public ArrowCameraTemplate2(ArrowCamera arrowCamera)  : base(arrowCamera) {
         this.arrowCamera.transform.parent = null;
         arrowCamera.transform.localPosition = new Vector3(8.33f, 2.45f, 6.4f);
-        arrowCamera.transform.localEulerAngles = new Vector3(0, -48, 0);
+        arrowCamera.transform.localEulerAngles = new Vector3(0, -42, 0);
     }
 
     bool isHit = false;

+ 2 - 2
Assets/BowArrow/Scripts/Manager/GameMgr.cs

@@ -116,13 +116,13 @@ public class GameMgr : MonoBehaviour
     public static float RealSizeToGameSize(float realSize)
     {
         // return realSize * 0.413966f;
-        return realSize * 0.7f;
+        return realSize;
     }
     //游戏场景的计量值转现实的计量值(米)
     public static float GameSizeToRealSize(float gameSize)
     {
         // return gameSize / 0.413966f;
-        return gameSize / 0.7f;
+        return gameSize;
     }
 }
 

+ 1 - 1
ProjectSettings/DynamicsManager.asset

@@ -4,7 +4,7 @@
 PhysicsManager:
   m_ObjectHideFlags: 0
   serializedVersion: 13
-  m_Gravity: {x: 0, y: -6.86, z: 0}
+  m_Gravity: {x: 0, y: -9.8, z: 0}
   m_DefaultMaterial: {fileID: 0}
   m_BounceThreshold: 2
   m_DefaultMaxDepenetrationVelocity: 10

部分文件因文件數量過多而無法顯示