[英]How to rotate door smoothly
I'm making a game where a door has to rotate -90 degrees smoothly.我正在制作一个门必须平滑旋转 -90 度的游戏。 I've tried lots of different things, but i can't make it work.我尝试了很多不同的东西,但我无法让它发挥作用。
public float degrees = -90f;
public Transform door;
private void update()
{
door.transform.Rotate(0f, 0f, degrees, Space.Self);
}
How do i make it rotate smoothly and not just snap into place?我如何使它平稳旋转而不是卡入到位?
A simple solution一个简单的解决方案
public float degrees = -90f;
public float rotationDuration = 5f;
public Transform door;
private float startTime;
private bool isOpenningDoor;
private void OpenDoor()
{
startTime = Time.time;
door.transform.eulerAngles = Vector3.zero;
isOpenningDoor = true;
}
private void update()
{
if(Input.GetKeyDown(KeyCode.O))//for testing
{
OpenDoor();
}
if(isOpenningDoor)
{
float ratio = (Time.time - startTime)/rotationDuration;//percentage of total rotation
if(ratio >= 1f)//when you reached max rotation
{
ratio = 1f;
isOpenningDoor = false;//Stop extra rotation
}
door.transform.Rotate(0f, 0f, ratio * degrees, Space.Self);
}
}
The open time is 5 secs in the example.示例中的打开时间为 5 秒。 If you want to do a lot of small programmer side animations I suggest Dotween for some nice easing and quick implementation.如果你想做很多小程序员的动画,我建议Dotween做一些很好的缓动和快速的实现。
You can do that by using Coroutines and Slerp method.您可以通过使用Coroutines和Slerp方法来做到这一点。 Try this:尝试这个:
using System.Collection;
...
public Transform door;
public float degrees = -90f;
public float someSpeed = 0.08f;
IEnumerator OpenDoor ()
{
while (door.transform.localEulerAngles.y > degrees)
{
door.transform.localEulerAngles = Vector3.Slerp(door.transform.localEulerAngles
, new Vector3(0, degress - 0.2f, 0), someSpeed);
yield return new WaitForEndOfFrame();
}
yield break;
}
And where you need, just call StartCoroutine(OpenDoor());
在您需要的地方,只需调用StartCoroutine(OpenDoor());
. .
Hope that's what you want.希望这就是你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.