随着时间的推移围绕轴心点旋转GameObject [英] Rotate GameObject around pivot point over time

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

问题描述

我正在使用以下代码相对于另一个枢轴点旋转一个点:

I am rotating a point in relation to another pivot point using the following code:

Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) {

    Vector3 dir = point - pivot; // get point direction relative to pivot
    dir = Quaternion.Euler(angles) * dir; // rotate it
    point = dir + pivot; // calculate rotated point
    return point;
}

如何使用协同程序在特定持续时间内旋转点(例如5秒)?

How can I rotate the point over a specific duration, say 5 seconds using a coroutine?

推荐答案

修改了我

Modified the code from my other answer to get this effect. You can read that to understand how lerp work.

发生了什么变化以及原因:

What changed and why:

1 .通过将Vector3.zero传递给angle参数和while循环外部的当前Object位置,从RotatePointAroundPivot函数获得beginRotation点.这需要做一次,因为结果将在while循环中使用.

1.Got the beginRotation point from the RotatePointAroundPivot function by passing Vector3.zero to the angle parameter and the current Object position outside of the while loop. This needs to be done once as the result will be used in the while loop.

Vector3 beginRotPoint = RotatePointAroundPivot(objPoint.transform.position, pivot, Vector3.zero);

之所以这样做是因为lerp需要一个起点.否则切勿更改该起点,否则它将破坏lerp功能.

This is done because lerp needs a starting point. That starting point should never changed otherwise, it break the lerp function.

2 .在while循环中每帧生成新角度.这是通过从Vector3.zero锁定到目标角度来完成的:

2.Generate new angle each frame in the while loop. This is done by lerping from Vector3.zero to the target angle:

float t = counter / duration;
Vector3 tempAngle = Vector3.Lerp(Vector3.zero, angles, t);

3 .找到最终的枢轴旋转角度.这是通过将起始点#1 传递给第一个参数,将枢轴点传递给第二个参数,最后将#2 产生的当前角度传递给第三个参数来完成的

3.Find the final pivot angle rotation. This is done by passing the starting point from #1 to the first parameter, the pivot point to the second parameter and finally the current angle generated from #2 to the third parameter.

Vector3 tempPivot = RotatePointAroundPivot(beginRotPoint, pivot, tempAngle);

4 .最后,将结果从#3 分配给objPoint.transform.position而不是objPoint.transform.eulerAngles,因为您不仅要移动对象.您还在旋转它.

4.Finally, assign the result from #3 to objPoint.transform.position instead of objPoint.transform.eulerAngles because you are not only moving the object. You are also rotating it.

objPoint.transform.position = tempPivot;


完整代码:


Complete code:

您的RotatePointAroundPivot函数:

Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
{
    Vector3 dir = point - pivot; // get point direction relative to pivot
    dir = Quaternion.Euler(angles) * dir; // rotate it
    point = dir + pivot; // calculate rotated point

    return point;
}

修改过的 rotateObject 函数,该函数描述了上面的更改:

Modified rotateObject function that I described what changed above:

bool rotating = false;

IEnumerator rotateObject(GameObject objPoint, Vector3 pivot, Vector3 angles, float duration)
{
    if (rotating)
    {
        yield break;
    }
    rotating = true;

    Vector3 beginRotPoint = RotatePointAroundPivot(objPoint.transform.position, pivot, Vector3.zero);

    float counter = 0;
    while (counter < duration)
    {
        counter += Time.deltaTime;

        float t = counter / duration;
        Vector3 tempAngle = Vector3.Lerp(Vector3.zero, angles, t);

        Vector3 tempPivot = RotatePointAroundPivot(beginRotPoint, pivot, tempAngle);
        objPoint.transform.position = tempPivot;
        Debug.Log("Running: " + t);
        yield return null;
    }
    rotating = false;
}


用法:

将在5秒钟内将对象绕枢轴旋转180度:

Will rotate object 180-deg around pivot point in 5 seconds:

//The GameObject with the pivot point to move
public GameObject pointToRotate;
//The Pivot point to move the GameObject around
public Transform pivotPoint;

//The angle to Move the pivot point
public Vector3 angle = new Vector3(0f, 180f, 0f);

//The duration for the rotation to occur
public float rotDuration = 5f;

void Start()
{
    StartCoroutine(rotateObject(pointToRotate, pivotPoint.position, angle, rotDuration));
}

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

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