Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Speed Up

Discussion in 'Scripting' started by Fersutagames_, Nov 22, 2015.

  1. Fersutagames_

    Fersutagames_

    Joined:
    May 2, 2013
    Posts:
    52
    Hey guys!

    Here's a copy of my script in hope you can help me.
    Code (JavaScript):
    1.  
    2. var speed : float = 5.0;
    3. function Update () {
    4. transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
    5. }
    What im trying to achieve is that every 2 seconds the speed will increase by 0.01f

    im not sure on how to do this though! Any help would be sweet!
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    The easiest way I find is to put the speed declaration at the top of the script & have a timer that counts down, when it is <=0 increment your speed float & reset the timer. While the timer is >0 decrease it by time.deltatime
     
  3. Fersutagames_

    Fersutagames_

    Joined:
    May 2, 2013
    Posts:
    52
    Any tips on how to implement this? I've never done any work with timers and adding values before :/

    Would it just be something like?

    Code (JavaScript):
    1. var speed : float = 5.0;
    2. var timer : float = 0.02;
    3.  
    4. function Update () {
    5.  
    6. yield WaitForSeconds(timer);
    7.  
    8. transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
    9. }
    If not could you correct me please :p

    Edit: Another Australian! ;D
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You want your timer =2f

    In update I then do

    If (timer>0) timer-= time.deltatime;
    Else if (timer<=0){
    Speed += 0.01f;
    Timer =2f;
    }

    Note my caps are probably wrong for this but you should get the idea.

    Edit, I used c# style, I don't know js.
     
    Fersutagames_ likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Too many around here ;)

    You can also use a coroutine or InvokeRepeating. Same concept, different implementations.
     
  6. Fersutagames_

    Fersutagames_

    Joined:
    May 2, 2013
    Posts:
    52
    Never to many! How hot is NSW but!

    sweet! ill take a look :)