简体   繁体   English

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

[英]how to make a gameObject rotate around another gameObject in Unity in the rotating plane

I've two gameObjects, sphereOne and sphereTwo as the children of an empty gameObject both .我有2个gameObjects, sphereOnesphereTwo为空游戏物体的孩子both

I've this C# code attached to a sphereTwo我将这个 C# 代码附加到一个sphereTwo

private void Update() =>
    transform.RotateAround(sphereOne.transform.position, 
        new Vector3(0, 1, 0), 100 * Time.deltaTime);

This lets sphereTwo rotate around sphereOne .这让sphereTwo围绕sphereOne旋转。

When I rotate the parent gameObject both , it rotates only on that specific plane.当我旋转父游戏物体both ,它只能在特定的平面旋转。

在此处输入图片说明

How do I dynamically update the transform position of the rotating sphere so that it lies on the same plane as the parent Object's rotation?如何动态更新旋转球体的变换位置,使其与父对象的旋转位于同一平面上?

The second parameter in Transform.RotateAround() is the axis which determines the orientation of the plane on which sphereTwo rotates around sphereOne . Transform.RotateAround()的第二个参数是确定sphereTwo围绕sphereOne旋转的平面方向的轴。 Right now, you have this set to a static value of new Vector3(0, 1, 0) , basically the world's up vector.现在,您已将此设置为new Vector3(0, 1, 0)的静态值,基本上是世界上的向上向量。

To have the axis instead be based on the orientation of sphereOne , use its Transform.up vector instead - this will change based on the world-space rotation of sphereOne 's transform:要让轴基于sphereOne的方向,请改用其Transform.up向量 - 这将根据sphereOne变换的世界空间旋转而改变:

private void Update() 
{
    transform.RotateAround(sphereOne.transform.position, sphereOne.transform.up, 100*Time.deltaTime);
}

(You could probably alternatively use both.transform.up , depending on the situation.) (您也可以根据情况使用both.transform.up 。)

Hope this helps!希望这有帮助! Let me know if you have any questions.如果您有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM