0

I would like to move an object and move it faster overtime, to be exact , increase its speed by 0.05f per second.

Below is a script attach to any object that will move from Right to left then to Right again.

using UnityEngine;

public class CountDownText : MonoBehaviour
{
    public float minimum = -1000.0F;
    public float maximum = 1000.0F;

    // starting value for the Lerp
    static float t = 0.0f;

    void Update()
    {
        // animate the position of the game object...
        transform.localPosition = new Vector3(Mathf.Lerp(minimum, maximum, t), 0, 0);

        // .. and increase the t interpolater
        t += 0.05f * Time.deltaTime;

        // now check if the interpolator has reached 1.0
        // and swap maximum and minimum so game object moves
        // in the opposite direction.
        if (t > 1.0f)
        {
            float temp = maximum;
            maximum = minimum;
            minimum = temp;
            t = 0.0f;
        }
    }
}

The movement is of fixed speed somehow, anything i do wrong? Thanks in advance!

Any error that i overlooked

2 Answers2

0

Extract 0.05f to a class field. float tExp = 0.05f

Change the line

t += tExp; // add the increasing amount
tExp += 0.05f  * Time.deltaTime; // "increase the increasing amount by 0.05f every second"

now, every next frame t will update with a different amount.

Phillip Zoghbi
  • 512
  • 3
  • 15
0

The object moves at a steady speed because the 3rd input is increasing at a steady speed. The way lerp works is it returns a value between the first and second parameters depending on the third.

If you want to increase the speed, you need to increase the t value exponentially.

Try this

float i=1;
t += i * Time.deltaTime;
i++;
Vionix
  • 570
  • 3
  • 8