Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不知道速度的情况下计算轨迹的角度_C#_Math_Unity3d_Angle - Fatal编程技术网

C# 如何在不知道速度的情况下计算轨迹的角度

C# 如何在不知道速度的情况下计算轨迹的角度,c#,math,unity3d,angle,C#,Math,Unity3d,Angle,在不知道速度的情况下,如何计算弹道击中目标的角度。我只知道最大高度、偏移高度和到目标的距离 这是我到目前为止得到的结果(我不知道如何计算偏移高度): 我用它来计算速度(很好,我只需要正确的角度): 我得到了解决方案: float GetAngle(float height, Vector3 startLocation, Vector3 endLocation) { float range = Mathf.Sqrt(Mathf.Pow(startLocation.x -

在不知道速度的情况下,如何计算弹道击中目标的角度。我只知道最大高度、偏移高度和到目标的距离

这是我到目前为止得到的结果(我不知道如何计算偏移高度):

我用它来计算速度(很好,我只需要正确的角度):

我得到了解决方案:

float GetAngle(float height, Vector3 startLocation, Vector3 endLocation)
    {
        float range = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));
        float offsetHeight = endLocation.y - startLocation.y;
        float g = -Physics.gravity.y;

        float verticalSpeed = Mathf.Sqrt(2 * gravity * height);
        float travelTime = Mathf.Sqrt(2 * (height - offsetHeight) / g) + Mathf.Sqrt(2 * height / g);
        float horizontalSpeed = range / TravelTime;
        float velocity = Mathf.Sqrt(Mathf.Pow(verticalSpeed,2) +  Mathf.Pow(horizontalSpeed, 2));

        return -Mathf.Atan2(verticalSpeed / velocity, horizontalSpeed / velocity) + Mathf.PI;
    }

看这里开始…非常好。别忘了接受它,让别人知道它已经解决了。
float LaunchVelocity (Vector3 startLocation, Vector3 endLocation, float angle)
 {
         float range = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));
         float offsetHeight = endLocation.y - startLocation.y;
         float gravity = Physics.gravity.y;

         float velocity = range * range * gravity;
         velocity /= range * Mathf.Sin(2 * angle) + 2 * offsetHeight * Mathf.Pow(Mathf.Cos(angle),2);
         return Mathf.Sqrt(velocity);
 }
float GetAngle(float height, Vector3 startLocation, Vector3 endLocation)
    {
        float range = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));
        float offsetHeight = endLocation.y - startLocation.y;
        float g = -Physics.gravity.y;

        float verticalSpeed = Mathf.Sqrt(2 * gravity * height);
        float travelTime = Mathf.Sqrt(2 * (height - offsetHeight) / g) + Mathf.Sqrt(2 * height / g);
        float horizontalSpeed = range / TravelTime;
        float velocity = Mathf.Sqrt(Mathf.Pow(verticalSpeed,2) +  Mathf.Pow(horizontalSpeed, 2));

        return -Mathf.Atan2(verticalSpeed / velocity, horizontalSpeed / velocity) + Mathf.PI;
    }