如何使游戏对象在旋转平面中围绕Unity中的另一个游戏对象旋转

pta*_*mzz 2 c# rotation unity-game-engine

我有两个gameObjects,sphereOne并且sphereTwo是一个空游戏对象的孩子both.

我把这个C#代码附加到了 sphereTwo

    void Update () 
   {
    transform.RotateAround(sphereOne.transform.position, new Vector3(0, 1, 0), 100*Time.deltaTime);
    }
Run Code Online (Sandbox Code Playgroud)

这让我们可以sphereTwo旋转sphereOne.

当我旋转父游戏对象时both,它仅在该特定平面上旋转.

在此输入图像描述

如何动态更新旋转球体的变换位置,使其与父对象的旋转位于同一平面上?

Ser*_*ite 6

第二个参数Transform.RotateAround()是轴,它决定了sphereTwo旋转的平面的方向sphereOne.现在,你将此设置为静态值new Vector3(0, 1, 0),基本上是世界的向上矢量.

要使轴取而代之sphereOne,请使用其Transform.up向量 - 这将根据sphereOne变换的世界空间旋转而改变:

void Update () 
{
    transform.RotateAround(sphereOne.transform.position, sphereOne.transform.up, 100*Time.deltaTime);
}
Run Code Online (Sandbox Code Playgroud)

(你可以选择使用both.transform.up,视情况而定.)

希望这可以帮助!如果您有任何疑问,请告诉我.