|
|
@@ -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() {
|