Unity 3D在一段时间内平稳旋转对象 [英] Unity 3D Rotate object smoothly during time
问题描述
我正在编写一个游戏,每次节拍器跳动时,立方体必须绕其自身平稳地旋转90度.
I'm programming a game where a cube has to rotate smoothly around itself 90 degrees each time a metronome is beating.
每次节拍器跳动时,我的方法称为协程:
Each time the metronome is beating, my method call a coroutine :
IEnumerator moveCoroutine()
{
if (!isCollided && canRotate) {
for (float i = 0; i < 10; i++) {
transform.RotateAround(pivot, direction, 90 / 10);
yield return null;
}
}
}
并且立方体旋转得相当平稳,但是旋转是在节拍器的下一个刻度之前很长时间完成的,我希望它以平稳的移动从头到尾旋转.
and the cube rotates rather smoothly, but the rotation is done long time before the next tick of the metronome, and I want it to rotate from the beginning to the end, in a smooth move.
我想我必须使用两个节拍器滴答声之间的时间(这是变种'Metronome.manager.delay'),但我没有找到方法.
I imagine that I've to use the time between two metronome ticks (which is the var 'Metronome.manager.delay') but I'm not finding how.
感谢您的帮助
推荐答案
因此,您的统一c#脚本通常会在每个帧渲染时运行.为了使动画更流畅,您需要根据文档将变化的字段/变量乘以Time.DeltaTime
:
So your unity c# script will usually run at each frame render. For your animation to be smooth, you need to multiply your changing field/variable with Time.DeltaTime
as per the documentation:
http://docs.unity3d.com/ScriptReference/Time-deltaTime.html
Time.DeltaTime
由统一游戏引擎计算,是从最后渲染的帧起经过的时间.
Time.DeltaTime
is calculated by the unity game engine and is the elapsed time from the last rendered frame.
文档中的示例:
public class ExampleClass : MonoBehaviour {
void Update() {
float translation = Time.deltaTime * 10;
transform.Translate(0, 0, translation);
}
}
在此示例中,转换在z轴上平移了10个单位,由于Time.deltaTime
[校正系数],该单位已正确设置了动画.
In the example, the transform is being translated on the z-axis by 10 units which is correctly animated due to the Time.deltaTime
[correction factor].
如果您在每个值上加上或减去一个值,则应该 与Time.deltaTime相乘.当您与Time.deltaTime相乘时 您实质上是这样表达的:我想将这个物体每次移动10米 秒,而不是每帧10米.
If you add or subtract to a value every frame chances are you should multiply with Time.deltaTime. When you multiply with Time.deltaTime you essentially express: I want to move this object 10 meters per second instead of 10 meters per frame.
另一个使用协程的示例:
Another example that uses a coroutine:
using UnityEngine;
using System.Collections;
public class CoroutinesExample : MonoBehaviour
{
public float smoothing = 1f;
public Transform target;
void Start ()
{
StartCoroutine(MyCoroutine(target));
}
IEnumerator MyCoroutine (Transform target)
{
while(Vector3.Distance(transform.position, target.position) > 0.05f)
{
transform.position = Vector3.Lerp(transform.position, target.position, smoothing * Time.deltaTime);
yield return null;
}
print("Reached the target.");
yield return new WaitForSeconds(3f);
print("MyCoroutine is now finished.");
}
}
您想要的可能是这样的:
What you want is probably something like this:
IEnumerator moveCoroutine()
{
if (!isCollided && canRotate) {
for (float i = 0; i < 10; i++) {
transform.RotateAround(pivot, direction, 9.0f * Time.deltaTime);
yield return null;
}
}
}
这可以修复动画的平滑度,并使动画独立于每台计算机的渲染帧(FPS)的能力. ->问题,只有在非您自己的设备上进行测试时,您才会注意到.
This fixes the smoothness of the animation and makes it independent of each computer's capacity to render frames (FPS). -> issue you will only notice when you playtest on other devices than your own.
现在解决动画长度的问题:
Now to fix the issue of the length of the animation:
IEnumerator moveCoroutine()
{
int maxIterations = 10;
if (!isCollided && canRotate) {
for (float i = 0; i < maxIterations; i++) {
transform.RotateAround(pivot, direction, 9.0f * Time.deltaTime);
yield return new WaitForSeconds(Metronome.manager.delay / maxIterations);
}
}
}
WaitForSeconds(Metronome.manager.delay / maxIterations);
将确保动画以所需的时间播放. (请注意,如果Metronome.manager.delay
尚未转换,则需要将其转换为秒.
The WaitForSeconds(Metronome.manager.delay / maxIterations);
will ensure your animation plays out with the timing you desire. (note that Metronome.manager.delay
will need to be converted to seconds if it is not already.
这篇关于Unity 3D在一段时间内平稳旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!