基于计时器生成对象

时间:2013-03-28 14:51:04

标签: c# unity3d

我正在尝试根据计时器在两个对象之间制作目标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;

        }

    }

此代码位于我的更新功能中,并附加到我想要上下移动的每个对象中。每个对象都能够独立于其他对象设置等待时间,因此我可以在5秒后进行1次移动,在10次后进行另一次等等。

然后,每个目标等待几秒钟然后向下移动。然而,运动不平滑并且倾向于跳跃设定距离。但是,当它回到谷底时,它会在_movedown bool和不会移动之间疯狂。

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

我知道Mathf.PingPong方法不断地将对象在两点之间移回,但这不允许我在每个部分暂停移动。但是,如果有人知道我可以这样做的方式,请告诉我。

3 个答案:

答案 0 :(得分:1)

这是对代码的快速破解。它可能更干净但它应该在紧要关头。我已经替换OP,如果/然后阻止w /'direction'变量,指示我们是从开始到结束还是结束到开始。

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

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

此示例是Vector3.Lerp documentation

中的示例的修改版本
// 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;
    }
}

我的'方向'成员可能很容易成为一个布尔,但我喜欢有签名方向用于其他目的。

答案 1 :(得分:0)

试试这个:

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米的速度执行这些移动。

答案 2 :(得分:0)

在下面添加了一些评论,可以帮助您澄清您的意图。

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?
    }
}