Unity3d随时间绕特定点旋转 [英] Unity3d Rotate around a specific point over time

查看:220
本文介绍了Unity3d随时间绕特定点旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数,可以在一段时间内将其精确地旋转90度.到目前为止,一切工作正常,我只需要弄清楚如何使其绕某个点旋转,而不是围绕变换中心旋转.

I'm writing a function that slowly turns exactly 90 degreees over the course of a period of time. So far everything is working fine, I just need to figure out how to make it rotate around a point instead of rotation around the transforms center.

protected bool _isRotating = false;
public IEnumerator RotateWithEasing(GameHelper.Axis axis, Vector3 isolatedAxisPoint, float inTime)
{
    if(_isRotating)
    {
        yield break;
    }

    _isRotating = true;

    var degrees = this.GetDegreesFromAxis(axis);

    Quaternion fromAngle = transform.rotation;
    Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + degrees);
    for (float t = 0f; t < 1f; t += Time.deltaTime / inTime)
    {
        transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
        yield return null;
    }

    _isRotating = false;
}

任何人都可以帮助我指出正确的方向,如何修改它,使其绕指定的isolatedAxisPoint旋转?

Can anyone help point me in the right direction, How can I modify this so it rotates around the specified isolatedAxisPoint?

推荐答案

这是一个实际的Unity补间示例,它恰巧在文本编辑器中显示在另一个窗口中!

Here's an actual example Unity tween that happened to be showing in a text editor another window!

如果要旋转某些内容,这很简单,只需在IEnumerator的循环中使用deltaTime,然后调整 eulerAngles 或仅调用 Rotate . (在Unity中,出于任何原因都不要使用四元数.)在此示例中,我仅调用Rotate.

If you want to rotate something, it is trivial just use deltaTime in a loop in an IEnumerator, and adjust the eulerAngles or just call Rotate. (Never use quaternions for any reason in Unity.) In this example I just call Rotate.

这是Unity中的非常基本的模式.

这是Unity中的补间的基本模式.您将在Unity中执行此操作数千次!

Here is the basic pattern of a tween in Unity. You will do this 1000s of times in Unity!

  1. 计算结束时间
  2. 做一会儿循环直到到达那个时间
  3. 每个框架,以适当的比例滑动(或其他).

请注意,您当然

 yield return null;

在循环内,它表示等到下一帧".

inside the loop, it means "wait until the next frame".

(请注意,与任何补间一样,最好在末尾强制设置最终值,这样您就知道它是完美的.注意,在此示例中,我只是在末尾设置了eulerAngles当循环结束时.)

(Note that as with any tween, it's good practice to force-set the final values at the end so you know it's perfect. Notice in this example I simply set the eulerAngles, at the end when the loop is finished.)

private IEnumerator _roll( Vector3 delta )
    {
    rollBegins.Invoke();
    
    Vector3 begin = transform.eulerAngles;
    
    float rollSecs = 2f; // speed of the roll
    
    float startTime = Time.time;
    float endTime = startTime+rollSecs;

    while (Time.time < endTime)
        {
        Vector3 d = delta * ( Time.deltaTime / rollSecs );
        transform.Rotate(d, Space.World );
        yield return null;
        }
    
    transform.eulerAngles = .. perfectly correct end values;
    
    busy = false;
    
    rollComplete.Invoke();
    }

{注意,在该实际代码示例中,"delta"被理解为仅在一个轴上;不必担心,这只是使用协程的补间的一个示例.}

{Note, in that actual code example, "delta" is understood to be only on one axis; don't worry about this, it's just an example of a tween using a coroutine.}

注意-当然,有两种方法" 可以进入循环.您可以计算少量,然后将其移动到该帧.或者,您可以只计算当时的新职位.由你决定.许多程序员认为后者更合乎逻辑,如果这样做的话.还要注意,您可以执行while循环直到经过一定时间,或者可以执行while循环直到到达目的地! (要非常注意浮点数的均等性.)选择权由您决定.

Note - of course there are "two ways" to go inside the loop. You can calculate the small amount you should move it that frame. Or, you can just calculate what the new position should be at that time. It's up to you. Many programmers think the latter is more logical, if so, do that. Note too that you can do the while loop until a certain time has passed, or, you can do the while loop until you reach your destination! (Being very careful about float equalities.) The choice is yours.

请注意,您经常会在这些补间中使用 Lerp 甚至更好的 SmoothStep .它在Unity中无处不在.一旦掌握了基本模式,就可以尝试一下.

Note that very very often you use Lerp or even better SmoothStep with such tweens. It is ubiquitous in Unity. Once you master the basic pattern, experiment with that.

请注意,在我的示例中,代码使用 UnityEvent 标记动画的开始和结束.这是非常普遍的.毕竟,通常情况下,一旦完成一些动作,您就必须继续执行其他操作.另一个很好的例子是,尽管有动画效果,但用户无法进行转向操作或游戏中遇到的任何情况.

Note that in my example the code is using UnityEvent to flag the beginning and end of the animation. This is extremely common. After all it's usually the case that once some movement finishes, you then have to go on and do something else. Another good example is, while something is animating, the user is blocked from say steering or whatever is the case in your game.

UnityEvent的优秀介绍 https://stackoverflow.com/a/36249404/294884 请投票:)

Excellent intro to UnityEvent https://stackoverflow.com/a/36249404/294884 pls vote it up :)

作记录.

在Unity中补间的真正高级方法是补间:

The truly advanced way to tween in Unity is to tweeng:

基本Tweeng代码库出现在此问题中.

要花点时间才能掌握它,但它功能强大到了令人难以置信的地步.令人惊讶的是,tweeng扩展仅是几行代码.

It takes a while to get the hang of that but it is incredibly powerful. It's mindboggling that the tweeng extension is only a few lines of code.

这篇关于Unity3d随时间绕特定点旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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