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

How would i do this?

Discussion in 'Scripting' started by Smotteh, Jun 22, 2016.

  1. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    using UnityEngine;
    using System.Collections;
    public class PlayerMotor : MonoBehaviour {
    private CharacterController controller;
    private float speed = 5.0f;
    public float timeWithoutCrash = 0.0f;
    // Use this for initialization
    void Start () {
    controller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update () {
    timeWithoutCrash += Time.deltaTime;
    controller.Move((Vector3.forward * speed) * Time.deltaTime);
    }

    }

    So how would i make it so when the 'timeWithoutCrash' has 1 second, the speed increases by 5 or so
    i tried
    if(timeWithoutCrash += 1.0f)
    {
    speed += 5.0f;
    }
    This didnt work so any ideas?
     
  2. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    First off, please use CodeTags, next try using Time.time instead of making your own version of it. Lastly to check use the greater than or equal to operator ( >= ).

    So something like this:
    Code (CSharp):
    1. if (Time.time >= 5)
    2. {
    3. //you have gone 5 seconds
    4. }
     
    Smotteh likes this.
  3. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Does it increase once, or once for every second?
    Code (csharp):
    1. float totalSpeed = timeWithoutCrash > 1f ? baseSpeed : fastSpeed;  // Just upgrade speed once.
    2. float totalSpeed = baseSpeed + timeWithoutCrash * acceleration; // Accelerate smoothly over time
    3. float totalSpeed = baseSpeed + Mathf.Floor(timeWithoutCrash) * acceleration; // Accelerate in 1s steps
     
    Smotteh likes this.
  4. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    Thanks Man
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class PlayerMotor : MonoBehaviour {
    5.     private CharacterController controller;
    6.     private float speed = 5.0f;
    7.     public float maxSpeed = 100.0f;
    8.     public bool hasCrashed = false;
    9.     // Use this for initialization
    10.     void Start () {
    11.         controller = GetComponent<CharacterController>();
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.       //  timeWithoutCrash += Time.deltaTime;
    17.         controller.Move((Vector3.forward * speed) * Time.deltaTime);
    18.         if (speed >= maxSpeed)
    19.         {
    20.             speed = maxSpeed;
    21.         }
    22.         if (Time.time >= 5)
    23.         {
    24.             speed += 5;
    25.         }
    26.         if (hasCrashed)
    27.         {
    28.             speed = 5.0f;
    29.         }
    30.     }
    31.    
    32. }
    33.  
    But it speeds to 150 almost instantly so how would i make it only go +5 speed every .5 seconds or so?
     
  5. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    Once every like .5 seconds or so i want it to increase by 5
     
  6. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Time.time is the time since the beginning of the game - it's always going up so it's going to directly work for your needs - i.e. once it goes over 5 seconds it will always be over 5 seconds. One way to handle this sort of thing is to think of it as a scheduled time in the future. So calculate the next time when the speed should increase and compare time against that...

    Code (CSharp):
    1.  
    2.  
    3. float nextSpeedIncrease;
    4.  
    5. void Start()
    6. {
    7.   nextSpeedIncrease = Time.time + 5.0f;
    8. }
    9.  
    10. void Update()
    11. {
    12.   if (hasCrashed)
    13.   {
    14.     speed = 5.0f;
    15.     nextSpeedIncrease = Time.time + 5.0f;
    16.   }
    17.  
    18.   if (Time.time >= nextSpeedIncrease)
    19.   {
    20.     speed += 5.0f;
    21.     nextSpeedIncrease = Time.time + 5.0f;
    22.   }
    23. }
    Something like that.
     
    RavenOfCode likes this.
  7. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Honestly for this try using InvokeRepeating, just call something like:
    Code (CSharp):
    1. void IncreaseSpeed  ()
    2. {
    3. speed += 5;
    4. }
    with:
    Code (CSharp):
    1. InvokeRepeating("IncreaseSpeed", 5, 5);
    To answer your question to the above code, you are checking every frame if 5 seconds has passed. So after 5 seconds every frame will increase. Try something like this:
    Code (CSharp):
    1. private float nextIncrease = 5;
    2.  
    3. if (Time.time >= nextIncrease)
    4. {
    5. nextIncrease += 5;
    6. //5 seconds has passed
    7. }
    Basically after 5 seconds passes it then checks if 10 has passed, then after that it checks if 15 has passed.

    Edit: The second answer is basically what David said, he just beat me to the reply button. :)
    Also the reason you should use InvokeRepeating is that it is more efficient than checking every frame with Update(). Lastly the use of a Coroutine using WaitForSeconds and a while loop could have the same effect.
     
    Dave-Carlile likes this.
  8. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    Thanks So Much Raven of Code, And Dave!
     
    Dave-Carlile and RavenOfCode like this.
  9. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    A potential issue with InvokeRepeating is when you change your speed back to the minimum after a crash. If the next InvokeRepeating is ready to go then you'll immediately increase your speed - i.e. it doesn't reset the repeat timer. You can use CancelInvoke to cancel it and then call InvokeRepeating again it so the timing works out. But for me at least that seems more hacky than coding things yourself. But... more than one way to skin a cat as they say - use what works best for you.
     
    RavenOfCode likes this.
  10. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class PlayerMotor : MonoBehaviour
    5. {
    6.     private CharacterController controller;
    7.     public float speed = 5.0f;
    8.     public float maxSpeed = 100.0f;
    9.     public bool hasCrashed = false;
    10.     float nextSpeedIncrease;
    11.  
    12.     void Start()
    13.     {
    14.         controller = GetComponent<CharacterController>();
    15.         nextSpeedIncrease = Time.time + 0.1f;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         controller.Move((Vector3.forward * speed) * Time.deltaTime);
    21.         if (speed >= maxSpeed)
    22.         {
    23.             speed = maxSpeed;
    24.         }
    25.         if (hasCrashed)
    26.         {
    27.             speed = 5.0f;
    28.             nextSpeedIncrease = Time.time + 0.1f;
    29.         }
    30.  
    31.         if (Time.time >= nextSpeedIncrease)
    32.         {
    33.             speed += 1.0f;
    34.             nextSpeedIncrease = Time.time + 0.1f;
    35.         }
    36.     }
    37. }
    38.  
    Thanks Fam This Works <3333
     
    Dave-Carlile and RavenOfCode like this.