简体   繁体   English

如何将一个对象旋转到另一个对象周围的特定位置? (基本上绕行到指定位置)

[英]How do I rotate an object to a specific position around another object? (Essentially orbiting to specified position)

I have a planet and a moon. 我有一颗行星和一颗月亮。 The moon is parented to a dummy object, which is located at the center of the planet (essentially 0,0,0). 月亮是一个虚拟对象的父对象,该对象位于行星的中心(基本上为0,0,0)。 The moon is free to rotate around (to any position on) the planet. 月亮可以绕地球自由旋转(到任何位置)。 It should maintain a constant distance from the planet. 它应该与行星保持恒定的距离。

I have a specific point in mind that I want to rotate the moon to, though it needs to stay upright. 我想将月亮旋转到某个特定点,尽管它需要保持直立。 That is, the moon should consistently point "up" relative to the planet's surface. 也就是说,月球应始终相对于行星表面指向“上方”。 Basically it's like a "moveTo" script only in my case the moon should "rotate" around the planet until it reaches the point I'm looking for. 基本上,这就像一个“ moveTo”脚本,仅在我这种情况下,月亮才应该围绕地球“旋转”,直到到达我要寻找的点为止。

Here's what I have so far, though I can't work out the correct logic to use: 这是我到目前为止的内容,尽管我无法算出要使用的正确逻辑:

Vector3 targetDir = moveToPos - moon.transform.position;

float angle = Vector3.Angle( targetDir, moon.transform.up );

dummy.transform.RotateAround (moveToPos, moon.transform.up, angle);

Am I thinking of this correctly? 我是否正确地考虑了这一点? Once I get this working, I'd like to feed the moon different Vector3 positions and have the moon rotate to them on the surface of the planet. 一旦完成这项工作,我便希望向月亮提供不同的Vector3位置,并让月亮在行星表面上向它们旋转。 I have searched for something similar but can't find what I'm looking for. 我已经搜索了类似的东西,但是找不到我想要的东西。

The marker displayed in this screenshot should say "Rotate here", but this is essentially what my scene looks like: 此屏幕快照中显示的标记应显示“ Rotate here”,但这实际上是我的场景:

标记显示月亮应移至何处

在此处输入图片说明

You've already made things a lot easier by nesting your moon inside an empty transform. 通过将月亮嵌套在一个空的变换中,您已经使事情变得容易得多。 If it's properly set up*, this means you won't have to directly manipulate the moon's transform - you just need to rotate the container object until it faces the target position. 如果设置正确*,这意味着您不必直接操纵月球的transform -您只需要旋转容器对象,直到它面对目标位置为止。

* By this, I mean the container object is at (0, 0, 0) relative to the planet, and the moon is only locally translated along the z-axis so it lines up with the container's transform.forward vector. * 这样,我的意思是容器对象相对于行星位于(0,0,0),并且月亮仅沿z轴局部平移,因此它与容器的transform.forward向量对齐。

The problem is easier to approach if we break it down into smaller steps: 如果将问题分解为更小的步骤,则更容易解决该问题:

  • Determining the target direction the container needs to face. 确定容器需要面对的目标方向。 We can get this by just subtracting the container's position from the target position. 我们可以通过从目标位置减去容器的位置来获得。
  • Calculating the required rotation for the container to face the target direction. 计算容器面向目标方向所需的旋转。 This is a good place for Quaternion.LookRotation() . 这是Quaternion.LookRotation()的好地方。
  • Rotating the container until its direction matches the target direction. 旋转容器,直到其方向与目标方向匹配。 Quaternion.Lerp() can be used to achieve this. Quaternion.Lerp()可用于实现此目的。

Here's how you might implement these steps: 您可以按照以下步骤执行这些步骤:

Quaternion targetRotation;
Quaternion startRotation;
float progress = 1;

void SetTargetPosition(Vector3 position)
{
    // Calculating the target direction
    Vector3 targetDir = position - dummy.transform.position;

    // Calculating the target rotation for the container, based on the target direction
    targetRotation = Quaternion.LookRotation(targetDir);
    startRotation = dummy.transform.rotation;

    // Signal the rotation to start
    progress = 0;
}

void Update()
{
    if (progress < 1)
    {
        // If a rotation is occurring, increment the progress according to time
        progress += Time.deltaTime;

        // Then, use the progress to determine the container's current rotation
        dummy.transform.rotation = Quaternion.Lerp(startRotation, targetRotation, progress);
    }
}

Note: If the moon is rotating too quickly (it will currently complete the rotation in ~1 second), just add less to progress every frame, eg. 注意:如果月亮自转太快(目前将在约1秒内完成自转),则只需增加一点即可使每一帧progress ,例如。 Divide it by a factor. 除以一个因子。

As long as the container is centered in the planet, the moon should remain a constant distance away from the surface at all times, and by this method will always keep a consistent "up" direction relative to the planet's surface. 只要容器位于行星的中心,月亮就应始终保持与地面的恒定距离,并且通过这种方法将始终相对于行星表面保持一致的“向上”方向。

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

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

相关问题 如何投掷物体并使它围绕球形旋转,就像在绕轨道旋转一样 - How to throw an object and have it rotate around a sphere as if it were orbiting 如何根据C#中的鼠标光标位置围绕另一个对象旋转GameObject - How to rotate a GameObject around another object according to the mouse cursor position in C# 围绕 object 旋转相机,轻松更改 position - Rotate camera around object, ease on change position 如何围绕特定的 object 和/或围绕鼠标单击 position 画一个圆圈? - How to draw a circle around specific object and/or around the mouse click position? 如何根据鼠标位置旋转对象的位置? - How to rotate the position of an object based on mouse position? 如何在 Unity 中另一个游戏对象的转换 position 实例化 object? - How do I Instantiate an object at the transform position of another GameObject in Unity? 如何将目标对象围绕轴 A 旋转不同的对象围绕轴 B 旋转多少? - How do I rotate a target object around axis A by how much a different object is rotated around axis B? 如何随机化对象的位置? - How do I randomize an object's position? 如何检查某个位置的物体? - How do I check for an object at a certain position? flutter 为什么我需要从另一个对象的 position 中减去一个对象的 position 才能找到方向? - flutter why do I need to subtract an object's position from another object's position to find direction?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM