Ver código fonte

射箭优化

lvjincheng 4 anos atrás
pai
commit
6f00e09e74

Diferenças do arquivo suprimidas por serem muito extensas
+ 720 - 95
Assets/BowArrow/Scenes/Game.unity


+ 2 - 2
Assets/BowArrow/Scripts/Bluetooth/ShootCheck.cs

@@ -293,8 +293,8 @@ public class ShootCheck : MonoBehaviour
             {
                 //加速度acc的单位是g,最后需要乘上
                 shootSpeed *= 9.80665f;
-                //积分出来的值还是太小,需要一个倍率
-                shootSpeed *= 20;
+                // shootSpeed *= 20;
+
                 string strShootSpeed = "初速度: " + shootSpeed + " 帧数: " + keyAccList.Count + "\n";
                 
                 string str1 = strShootSpeed + "/////////检测到射出的数据////////////:\n";

+ 14 - 6
Assets/BowArrow/Scripts/Game/ArmBow.cs

@@ -124,10 +124,9 @@ public class ArmBow : MonoBehaviour
     }
 
     void shoot() {
-        RaycastHit raycastHit = CrossHair.ins.GetRaycastHit();
-
         // 筛选出一个稳定的发射角度---start
-        Quaternion best_rotation = this.bowCamera.transform.rotation;
+        Quaternion best_rotation = this.bowCamera.transform.rotation; //最终的结果包含shootBackTime处理
+        Quaternion best_best_rotation = this.bowCamera.transform.rotation; //最终的结果不包含shootBackTime处理
         if (recordCount >= recordRotations.Length) {
             int single_check_count = 6;
             float min_wave = float.MaxValue;
@@ -144,13 +143,23 @@ public class ArmBow : MonoBehaviour
                     if (wave < min_wave)
                     {
                         min_wave = wave;
-                        int best_rotation_index = i - single_check_count + 1 - this.shootBackTime;
+                        int best_rotation_index = i - single_check_count + 1;
+                        best_best_rotation = recordRotations[best_rotation_index];
+                        best_rotation_index -= this.shootBackTime;
                         if (best_rotation_index < 0) best_rotation_index = 0;
                         best_rotation = recordRotations[best_rotation_index];
                     }
                 }
             }
         }
+
+        Quaternion oldCameraRotation = Camera.main.transform.rotation;
+        Camera.main.transform.rotation = best_best_rotation;
+        RaycastHit raycastHit = CrossHair.ins.GetRaycastHit();
+        Camera.main.transform.rotation = best_rotation;
+        RaycastHit raycastHit1 = CrossHair.ins.GetRaycastHit();
+        Camera.main.transform.rotation = oldCameraRotation;
+
         // if (recordCount >= recordRotations.Length) {
         //     int single_check_count = 6;
         //     float min_wave = float.MaxValue;
@@ -175,8 +184,6 @@ public class ArmBow : MonoBehaviour
         // 筛选出一个稳定的发射角度---end
         
         Vector3 shootOutPosition = this.bowCamera.transform.position;
-        //如果弓指向太过朝下,在弓口发射可能会射穿地面,因此需要角度适当时,才从弓口射出。
-        if (BowCamera.ins.transform.localEulerAngles.x < 30) shootOutPosition += this.bowCamera.transform.forward * 1.5f;
         GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, best_rotation);
 
         Vector3 s1 = arrowCopy.transform.localScale;
@@ -189,6 +196,7 @@ public class ArmBow : MonoBehaviour
         arrowComp.armBow = this;
         arrowComp.shootOutPosition = shootOutPosition;
         arrowComp.raycastHit = raycastHit;
+        arrowComp.raycastHit1 = raycastHit1;
         // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
         // Arrow.speed = BaseSpeedSlider.ins.getValue();
         // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));

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

@@ -9,6 +9,7 @@ public class Arrow : MonoBehaviour
     public float flyTime = 0;
     public bool isHit = false;
     public RaycastHit raycastHit;
+    public RaycastHit raycastHit1;
     private float mySpeed = 0;
     public bool simulatedParabola = false; //模拟抛物线 
     public ArmBow armBow;
@@ -30,10 +31,12 @@ public class Arrow : MonoBehaviour
 
         //瞄准靶子发射时,自动调整仰角
         if (raycastHit.transform && raycastHit.transform.gameObject.name == "TargetBody") {
-            ApplyParabolaAngle(raycastHit.point);            
-            float distance = TargetBody.ins.GetDistance();
-            if (Mathf.RoundToInt(distance) >= 50) {
-                if (Random.value < 0.5) simulatedParabola = true;
+            if (raycastHit1.transform && raycastHit1.transform.gameObject.name == "TargetBody") {
+                ApplyParabolaAngle(raycastHit1.point);            
+                float distance = TargetBody.ins.GetDistance();
+                if (Mathf.RoundToInt(distance) >= 50) {
+                    if (Random.value < 0.5) simulatedParabola = true;
+                }
             }
 
             //把瞄准点画成红圈,渲染在靶子上
@@ -54,32 +57,25 @@ public class Arrow : MonoBehaviour
 
     void ApplyParabolaAngle(Vector3 destination)
     {
-        float angleX = 0;
-        Vector3 backVec = -this.transform.forward * 0.5f; //如果实际落点在上下方向出现了偏移,可以调整该参数
-        float distX = Vector2.Distance(
+        Vector3 backVec = -this.transform.forward * 0.8f; //如果因为物理系统误差导致落点出现上下偏移,可以调整该参数
+        float deltaX = Vector2.Distance(
             new Vector2(destination.x, destination.z), 
             new Vector2(this.transform.position.x + backVec.x, this.transform.position.z + backVec.z)
         );
-        float distY = destination.y - (this.transform.position.y + backVec.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
-        {
-            return;
-        }
-
-        Vector3 finalAngle = this.transform.eulerAngles;
-        finalAngle = new Vector3(-angleX, finalAngle.y, finalAngle.z);
-        this.transform.localRotation = Quaternion.Euler(finalAngle);
+        float deltaY = destination.y - (this.transform.position.y + backVec.y);
+        float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(this.mySpeed, 2);
+        float b = deltaX;
+        float c = a - deltaY;
+        if (Mathf.Pow(b, 2) - 4 * a * c >= 0) {
+            float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
+            float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
+            float angle1 = Mathf.Atan(res1) / Mathf.PI * 180;
+            float angle2 = Mathf.Atan(res2) / Mathf.PI * 180;
+            float angleX = Mathf.Min(angle1, angle2);
+            Vector3 finalAngle = this.transform.eulerAngles;
+            finalAngle = new Vector3(-angleX, finalAngle.y, finalAngle.z);
+            this.transform.localRotation = Quaternion.Euler(finalAngle);
+        }   
     }
 
     void OnDestroy() {

+ 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, -56, 0);
+        arrowCamera.transform.localEulerAngles = new Vector3(0, -48, 0);
     }
 
     bool isHit = false;

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

@@ -52,7 +52,7 @@ public class TargetBody : MonoBehaviour
     public void SetDistance(float value) {
         distance = value;
         Vector3 v3 = this.transform.parent.localPosition;
-        v3.x = 9.815f - GameMgr.RealSizeToGameSize(value);
+        v3.x = 30 - GameMgr.RealSizeToGameSize(value);
         this.transform.parent.localPosition = v3;
     }
 

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

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

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff