我正在尝试制作一个指向 Unity 程序中自定义位置的指南针。但是它完全关闭了,它通常指向错误的方向,我似乎无法弄清楚我的逻辑有什么问题。
我在这里使用弯曲几何轴承方程:http: //www.yourhomenow.com/house/havesine.html
- 获取当前位置。
- 计算从当前位置到目标位置的方位。
相对于真北旋转屏幕指南针。
// Calculate bearing between current location and target location // Get current GPS location float lat1 = Input.location.lastData.latitude; float lon1 = Input.location.lastData.longitude; float lat2 = TargetLatitude; float dLon = TargetLongitude - lon1; // Calculate bearing var y = Mathf.Sin(dLon) * Mathf.Cos(lat2); var x = Mathf.Cos(lat1) * Mathf.Sin(lat2) - Mathf.Sin(lat1) * Mathf.Cos(lat2) * Mathf.Cos(dLon); var brng = ((Mathf.Atan2(y, x)*(180.0f/Mathf.PI)) + 360.0f) % 360.0f; Dump.text = brng.ToString(); // Rotate the to target location relative to north. Z axis pointing out of screen. transform.eulerAngles = new Vector3(0, 0, Mathf.MoveTowardsAngle(transform.localEulerAngles.z, Input.compass.trueHeading + brng, COMPASS_MAXDELTA));
如果我删除该代码中的方位偏移,它指向的北方就足够用于数字罗盘了。