rita sharon
rita sharon

Reputation: 59

how to gradually increase camera speed moving upwards in unity

I want to create a game in which the camera moves in the y-axis (upwards) with gradually increasing speed.

void Update () {
        float translation = 0.5f;
        transform.Translate (0, translation, 0);

I used this code, but I want to increase the rate of speed

Upvotes: 0

Views: 819

Answers (3)

Hellium
Hellium

Reputation: 7346

public float BaseTranslationSpeed = 0.5f ;
public float TranslationSpeedIncreaseRate = 1 ;
private float translationSpeed ;

void Start()
{
    translationSpeed = BaseTranslationSpeed ;
}

void Update ()
{
    translationSpeed += TranslationSpeedIncreaseRate ;
    // Multiplying by Time.deltaTime is advised in order to be frame independant
    transform.Translate (0, translationSpeed * Time.deltaTime , 0);
}

You can even use an Animation curve to control the speed :

public AnimationCurve SpeedOverTime ; // Change the curve in the inspector
private float startTime ;

void Start()
{
    startTime = Time.time ;
}

void Update ()
{
    // Multiplying by Time.deltaTime is advised in order to be frame independant
    transform.Translate (0, SpeedOverTime.Evaluate( Time.time - startTime ) * Time.deltaTime , 0);
}

Upvotes: 1

ryeMoss
ryeMoss

Reputation: 4333

One method would be to create a timer and increment the speed every time X amount of seconds has passed:

float translation = 0.5f;
float timer = 0f;

void Update()
{
    timer += Time.deltaTime;
    if (timer > 1f) //after 1 second has passed...
    {
        timer = 0; // reset timer
        translation += 0.5f; //increase speed by 0.5
    }

    transform.Translate (0, translation, 0);
}

Upvotes: 1

Dave
Dave

Reputation: 2842

Functions like Vector3.Lerp(), Vector3.MoveTowards and Mathf.Lerp(), Mathf.MoveTowards() will help you do just that.

You can also multiply your translation by Time.deltaTime and control the speed by controlling the translation value.

Upvotes: 1

Related Questions