3

我正在开发 Unity3D 中的 2D 游戏(使用 Orthello 2D)。

由于我从 Cocos2d 和 CoronaSDK 切换,我想知道是否有一种方法可以为 sprite(或任何 Unity3D 对象)实现以下行为,因为它在 Corona 中工作:

object = ...
transition.to ( object, { time = 1000, rotation = object.rotation + 100, onComplete = function () 
    // do something
end })

所以一个精灵在 1 秒内旋转了 100 度。

在我附加到精灵的脚本中,我可以在我的Update ()函数中进行旋转,但这是一种有点不同的方法......

4

2 回答 2

1

您可以在更新功能中轻松完成。

float timer = 0f;

void Update()
{
    if(timer <= 1)
    {
// Time.deltaTime*100 will make sure we are moving at a constant speed of 100 per second
        transform.Rotate(0f,0f,Time.deltaTime*100);
// Increment the timer so we know when to stop
        timer += Time.deltaTime;
    }
}

如果您需要再旋转 100 度,则只需重置计时器。

您可以在此处查看不同版本的 Rotate 功能,并在此处查看有关救生员Time.deltaTime值的更多信息

于 2017-03-18T01:33:37.847 回答
0

有几种不同的方法可以做到这一点。例如使用协程:

IEnumerator TweenRotation(Transform trans, Quaternion destRot, float speed, float threshold )
{
  float angleDist = Quaternion.Angle(trans.rotation, destRot);

  while (angleDist > threshold)
  {
    trans.rotation = Quaternion.RotateTowards(trans.rotation, destRot, Time.deltaTime * speed);
    yield return null;

    float angleDist = Quaternion.Angle(trans.rotation, destRot);
  }
}
于 2013-09-28T17:55:45.537 回答