如何将对象旋转到另一个对象周围的特定位置?(本质上是绕到指定位置) [英] How do I rotate an object to a specific position around another object? (Essentially orbiting to specified position)

查看:46
本文介绍了如何将对象旋转到另一个对象周围的特定位置?(本质上是绕到指定位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一颗行星和一颗月亮.月亮是一个虚拟对象的父对象,该虚拟对象位于行星的中心(基本上是 0,0,0).月球可以围绕(到任何位置)行星自由旋转.它应该与行星保持恒定的距离.

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). The moon is free to rotate around (to any position on) the planet. It should maintain a constant distance from the planet.

我想将月亮旋转到一个特定的点,尽管它需要保持直立.也就是说,月球应该始终相对于行星表面向上".基本上它就像一个moveTo"脚本,只是在我的情况下,月亮应该围绕地球旋转",直到它到达我正在寻找的点.

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.

这是我到目前为止所拥有的,但我无法找出要使用的正确逻辑:

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);

我的想法正确吗?一旦我开始工作,我想为月球提供不同的 Vector3 位置,并让月球在地球表面旋转到它们.我搜索过类似的东西,但找不到我要找的东西.

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. 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:

推荐答案

通过将月亮嵌套在空变换中,您已经使事情变得容易了很多.如果设置正确*,这意味着您不必直接操作月球的transform - 您只需要旋转容器对象直到它面向目标位置.

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.

*通过这个,我的意思是容器对象相对于行星在 (0, 0, 0) 处,并且月球仅沿 z 轴局部平移,因此它与容器的 transform.forward 向量.

如果我们把问题分解成更小的步骤,这个问题就更容易解决:

The problem is easier to approach if we break it down into smaller steps:

  • 确定容器需要面对的目标方向.我们可以通过从目标位置中减去容器的位置来得到这个.
  • 计算容器面向目标方向所需的旋转.这是Quaternion.LookRotation().
  • 旋转容器,直到其方向与目标方向匹配. Quaternion.Lerp() 可用于实现此目的.
  • 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().
  • Rotating the container until its direction matches the target direction. Quaternion.Lerp() can be used to achieve this.

以下是您可以如何实施这些步骤:

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);
    }
}

注意:如果月亮旋转得太快(它目前会在大约 1 秒内完成旋转),只需在每一帧的 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. 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.

这篇关于如何将对象旋转到另一个对象周围的特定位置?(本质上是绕到指定位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆