C# 控制飞船绕目标旋转

C# 控制飞船绕目标旋转,c#,unity3d,entity-component-system,C#,Unity3d,Entity Component System,我试图让我的飞船实体要么朝着它们移动的方向看,要么朝着它们环绕的目标看。在这一点上,我会对任何一个结果感到高兴。不幸的是,尽管我在谷歌上做了很多关于如何做到这一点的研究,但我似乎对底层的数学理解不够 这就是我目前在我的旋转系统中所拥有的 public void Execute(ref Translation shipPosition, ref Ship ship, ref Rotation rotation) { ship.Theta += .0075f; float3

我试图让我的飞船实体要么朝着它们移动的方向看,要么朝着它们环绕的目标看。在这一点上,我会对任何一个结果感到高兴。不幸的是,尽管我在谷歌上做了很多关于如何做到这一点的研究,但我似乎对底层的数学理解不够

这就是我目前在我的旋转系统中所拥有的

public void Execute(ref Translation shipPosition, ref Ship ship, ref Rotation rotation)
{

      ship.Theta += .0075f;
      float3 delta = new float3(
          ship.Radius * Mathf.Sin(ship.Theta) * Mathf.Cos(ship.Phi),
          ship.Radius * Mathf.Cos(ship.Theta),
          ship.Radius * Mathf.Sin(ship.Theta) * Mathf.Sin(ship.Phi));


      rotation.Value = Quaternion.Slerp(rotation.Value,
          Quaternion.LookRotation(ship.OrbitTarget + delta-shipPosition.Value),0.5f);


      shipPosition.Value = ship.OrbitTarget + delta;           

}
这样做的结果是,船舶有1/2的时间面向其轨道目标,另一半的时间则远离目标。

如果没有第二个参数,
四元数.LookRotation
假设您希望本地
up
尽可能接近
Vector3.up
。相反,使用从轨道体到轨道体的方向:

public void Execute(ref Translation shipPosition, ref Ship ship, ref Rotation rotation)
{

    ship.Theta += .0075f;
    float3 delta = new float3(
            ship.Radius * Mathf.Sin(ship.Theta) * Mathf.Cos(ship.Phi),
            ship.Radius * Mathf.Cos(ship.Theta),
            ship.Radius * Mathf.Sin(ship.Theta) * Mathf.Sin(ship.Phi));

    Vector3 previousPos = shipPosition.Value;

    shipPosition.Value = ship.OrbitTarget + delta;   

    rotation.Value = Quaternion.Slerp(rotation.Value, Quaternion.LookRotation(
            shipPosition.Value - previousPos,
            ship.OrbitTarget - shipPosition.Value), 0.5f);
}