编写一个基于计时器的对象

本文关键字:计时器 对象 一个 | 更新日期: 2023-09-27 18:06:25

我正在尝试基于计时器在两个对象之间创建一个目标lerp。

目前,我有以下代码:

   float distCovered = (Time.time - waitTime) * speed;
    float fracJourney = distCovered / journeyLength;
    if (_moveDown == false)
    {
        if (startTime + waitTime < Time.time)
        {
            transform.position = Vector3.Lerp(start.position, end.position, fracJourney);
            if (transform.position == end.position)
            {
                Debug.Log("going down");
               _moveDown = true;
                transform.position = Vector3.Lerp(end.position, start.position, fracJourney);
            }

        }
    }
    if (_moveDown == true)
    {
        float distCovered1 = (Time.time - goDowntimer) * speed;
        float fracJourney1 = distCovered1 / journeyLength;
        transform.position = Vector3.Lerp(end.position, start.position, fracJourney1);
        if (transform.position == start.position)
        {
            Debug.Log("going up");
           // waitTime = 20;
            _moveDown = false;
        }
    }

这段代码位于我的update函数中,并附加到我想上下移动的每个对象上。每个对象都能够独立于其他对象设置其等待时间,因此我可以在5秒后移动一个,在10秒后移动另一个等。

然后,每个目标等待几秒钟并向下移动。然而,这个动作并不流畅,它往往会跳跃一定的距离。但是,当它回到底部时它会在_movedown bool和不移动之间发疯。

有谁知道我可以解决这些问题的方法吗?

我知道数学。乒乓的方法,不断地将物体在两点之间向后移动,但这不允许我在每个部分暂停运动。不过,如果有人知道我能做到这一点的方法,请告诉我。

编写一个基于计时器的对象

下面是对代码的快速破解。它可能会更干净,但它应该在紧要关头。我已经替换了OPs if/then块w/一个"方向"变量,指示我们是从开始移动到结束还是从结束移动到开始。

Vector3.Lerp()取t值,取值范围为[0,1],从起点到终点的距离为%。如果你想反转这个方向,你所需要做的就是从1中减去1,使范围变成[1,0](反向)。这就是我在direction_下面所做的一切。一旦fracJourney超出范围,我们就会切换方向,触发暂停,并重置主计时器。

我将暂停代码放在Update()中以将其与移动代码分开,但没有任何理由两个代码块都不能在FixedUpdate()或Update()中。

这个例子是Vector3中的一个修改版本。昆虫蜜文档:

// additional data members beyond Vector3.Lerp's documentation
public float PauseTime = 2.0f;
int direction_ = 1;
bool doPause_ = false;
void Update(){
    float elapsedTime = Time.time - startTime;
    //  if the elapsed time has exceeded the pause time and we're paused
    //  unpause and reset the startTime;
    if(elapsedTime > PauseTime && doPause_){
        doPause_ = false;
        startTime = Time.time;
    }
}
void FixedUpdate(){
    if(doPause_)return;
    float distCovered = (Time.time - startTime) * Speed;
    float fracJourney = distCovered / journeyLength;
    // +direction means we are going from [0,1], -direction means [1,0]
    fracJourney = (direction_>0)?fracJourney:1.0f-fracJourney;
    transform.position = Vector3.Lerp(StartPt.position, EndPt.position, fracJourney);
    // When fracJourney is not in [0,1], flip direction and pause
    if(fracJourney > 1.0 || fracJourney < 0.0){
        direction_ = -direction_;
        startTime = Time.time;
        doPause_ = true;
    }
}

我的'direction'成员也可以很容易地是bool类型,但我喜欢在其他用途上使用带符号的direction。

我在下面添加了一些评论,可能会帮助你澄清你的意图。

float distCovered = (Time.time - waitTime) * speed;
float fracJourney = distCovered / journeyLength;
// Going up...
if (_moveDown == false)
{
    // Should we be checking this in the other half of the statement too?
    // Or, outside the conditional altogether?
    if (startTime + waitTime < Time.time)
    {
        transform.position = Vector3.Lerp(start.position, end.position, fracJourney);
        if (transform.position == end.position)
        {
            Debug.Log("going down");
           // The way this is structured, we're going to *immediately* fall into the
           // following block, even if that's not your intended behavior.
           _moveDown = true;
            // Going down, but with the fracJourney value as though we were going up?
            transform.position = Vector3.Lerp(end.position, start.position, fracJourney);
        }
    }
}
// As noted above, we're going to fall directly into this block on the current pass,
// since there's no *else* to differentiate them.
if (_moveDown == true)
{
    // Doesn't follow the same pattern as in the previous block, though that may be intended
    float distCovered1 = (Time.time - goDowntimer) * speed;
    float fracJourney1 = distCovered1 / journeyLength;
    transform.position = Vector3.Lerp(end.position, start.position, fracJourney1);
    if (transform.position == start.position)
    {
        Debug.Log("going up");
       // waitTime = 20;
        _moveDown = false;
       // Should there be a Lerp here, as above, to start heading back the other way again?  Or, do you need to remove the other one?
    }
}

试试这个:

transform.position = Vector3.Lerp(start.position, end.position, fracJourney * Time.deltaTime);

或者甚至是这个

transform.position = Vector3.Lerp(start.position, end.position, speed * Time.deltaTime);

x *Time.deltaTime是这种情况,基本上指示移动方法以每秒x米的速度移动对象。如果没有deltaTime,它将以每帧x米的速度执行这些移动。