简体   繁体   English

按下按钮即可平滑Lerp对象

[英]Smoothly Lerp object based on button press Unity

Im creating a speedometer in Unity and I want my speedo arrow to smoothly lerp up to it's max angle based on the wither or not a button is pressed on. 我在Unity中创建了一个测速仪,我希望我的speedo箭头根据是否枯萎而平滑地弯曲到最大角度。 When the button is let go, I'd like the arrow to fall back to it's original rotation value. 放开按钮时,我希望箭头退回到其原始旋转值。

However, I'm having issues trying to work out how to make my object lerp from its original position, to it's new position and back again. 但是,我在尝试解决如何使对象从其原始位置变回新位置并再次返回时遇到问题。 I've got it so when I press a button the objects jumps to a position, however, I need it to be smoother. 我已经知道了,所以当我按下按钮时,对象会跳到某个位置,但是,我需要使其更加平滑。 Could someone please look over my code and point out what I need to do please? 有人可以查看我的代码并指出我需要做什么吗?

float maxAngle = 100.0f;
float maxVel = 200.0f;
Quaternion rot0;
public float rotateValue;

// Use this for initialization
void Start () 
{
    rot0 = transform.localRotation;
}

// Update is called once per frame
void Update ()
{
    if(Input.GetKey(KeyCode.Space))
    {
        SetNeedle(rotateValue);
    }
}

void SetNeedle(float vel)
{
    var newAngle = vel * maxAngle / maxVel;

    transform.localRotation = Quaternion.Euler(newAngle,0,0) * rot0;


    float angle = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
    transform.eulerAngles = new Vector3(0, angle, 0);
}

From the documentation : 文档中

static float Lerp(float from, float to, float t);
Interpolates between a and b by t. t is clamped between 0 and 1.

Which means t goes from 0% to 100%. 这意味着t从0%变为100%。 When t == 0.5f the result will be the middle angle between from to to . t == 0.5f结果将是之间的中间角fromto

Hence, if the needle needs to be moved in a smooth and constant speed, you should do: 因此,如果需要以平稳且恒定的速度移动针,则应该执行以下操作:

time += Time.deltaTime; // time is a private float defined out of this method
if(time >= 1.5f) // in this example the needle takes 1.5 seconds to get to its maximum position.
    time = 1.5f // this is just to avoid time growing to infinity.
float angle = Mathf.LerpAngle(from, to, time/1.5f); // time/1.5f will map the variation from 0% to 100%.

But that requires the from and to to be constant during the lerp fase. 但是,这需要在lerp fase过程中fromto保持恒定。 So what you need to do is the following: 因此,您需要执行以下操作:

  • when you press Space, set time = 0 , from = currentAngle and to = maxAngle ; 当您按Space键时,将time = 0设置time = 0from = currentAngle to = maxAngle Obviously this must be done only once during the key down event. 显然,在按键事件期间只能执行一次此操作。
  • when you release Space, set time = 0 , from = currentAngle and to = 0 . 释放Space时,将time = 0设置time = 0from = currentAngle to = 0 Also do this only once. 也只能这样做一次。

All right? 好吧?

You should use Time.deltaTime, not Time.time. 您应该使用Time.deltaTime,而不是Time.time。 That should make it move correctly. 那应该使它正确移动。

EDIT: deltaTime won't solve the problem alone, however. 编辑:deltaTime不能单独解决问题,但是。 Sorry I didn't fully read your code at first. 抱歉,我一开始没有完全阅读您的代码。

In order to smoothly move the needle, you need to know where the needle is currently, where it needs to go, and how much time has elapsed since the last time it moved. 为了使针头平稳移动,您需要知道针头当前在哪里,它需要去哪里以及自上次移动以来已经经过了多少时间。

We know how much time has elapsed with Time.deltaTime. 我们知道Time.deltaTime经过了多少时间。 But your code currently doesn't take into consideration where the needle is. 但是您的代码当前未考虑针的位置。 float angle = Mathf.LerpAngle(minAngle, maxAngle, Time.deltaTime); will always evaluate to roughly the same value, so your needle will never move beyond a tiny bit. 总是会得到大致相同的值,因此您的针头永远不会移动到一点点。

What you should do instead is lerp from the needle's current position towards the maxAngle. 您应该做的是从针的当前位置朝着maxAngle方向移动。

float angle = Mathf.LerpAngle(currentAngle, maxAngle, Time.deltaTime);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM