Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Velocity * Time.deltaTime

Discussion in 'Scripting' started by gore23, Jun 1, 2011.

  1. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    I made a script (below) and I was wondering how I would use delta time on it I cant get it to work. I did good with moving non rigid objects but I cant figure out the right way XD unless you dont need to for velocity?




    Code (csharp):
    1.  
    2. var speed : float = 1.0;
    3.  
    4. function Update () {
    5.    
    6.     if(Input.GetButtonDown("Forward")){
    7.        
    8.         rigidbody.velocity = Vector3(0,0,speed);
    9.        
    10.        
    11.     }
    12.    
    13. }
     
  2. 95KillerZ95

    95KillerZ95

    Joined:
    May 27, 2011
    Posts:
    253
    Hey, try with this :

    Code (csharp):
    1.  
    2. var speed : float = 1.0;
    3.  
    4. function Update () {
    5.    
    6.     if(Input.GetButton("Forward")){
    7.        
    8.         rigidbody.velocity = Vector3(0,0,speed*Time.deltaTime);
    9.        
    10.        
    11.     }
    12.    
    13. }
    14.  
    Say me if it works...
     
  3. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    that works Howabout for this addforce?
    because it says get_deltaTime must be called from main thread every time i try multiply speed by Time.deltaTime


    Code (csharp):
    1.  
    2. var speed : float = 3.0;
    3.  
    4. function Update () {
    5.    
    6.     if(Input.GetButton("Forward")){
    7.        
    8.         rigidbody.AddForce (0, 0, speed*Time.deltaTime);
    9.        
    10.     }
    11.    
    12.     if(Input.GetButton("Backward")){
    13.        
    14.         rigidbody.AddForce (0, 0, -speed);
    15.        
    16.     }
    17.    
    18.     if(Input.GetButton("Left")){
    19.        
    20.         rigidbody.AddForce (-speed, 0, 0);
    21.        
    22.     }
    23.    
    24.     if(Input.GetButton("Right")){
    25.        
    26.         rigidbody.AddForce (speed, 0, 0);
    27.        
    28.     }
    29.    
    30. }
     
  4. 95KillerZ95

    95KillerZ95

    Joined:
    May 27, 2011
    Posts:
    253
    Why use all those if???
    You can use this:

    Code (csharp):
    1.  
    2. var speed : float = 10;
    3. function Update()
    4. {
    5. var direction : Vector3 = Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
    6.  
    7. rigidbody.AddForce(direction*speed);
    8. }
    9.  
    10.  
     
  5. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Actually, you don't want to use Time.deltaTime with rigidbody.velocity. Due to the fact that it already moves your character at a speed that is framerate independent, using Time.deltaTime actually breaks things. It basically becomes framerate dependent again due to your velocity essentially being speed * Time.deltaTime * Time.deltaTime.

    So, just set rigidbody.velocity equal to Vector3(0,0,speed). It should work just fine.
     
  6. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    I just figured it was a little more friendly if i used those to gamers that want to doodle with controls (if they wanted to) than putting the pieces together about horizontal + and - ( I for one always see in controls forward backward left and right)
     
  7. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Nope.

    1) If it is physics based use FixedUpdate().

    2) Feel free to continue using Time.deltaTime - it allows you to alter timescale (e.g. slow motion).
     
    Last edited: Jun 1, 2011
  8. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Time.timeScale also alters velocity without using Time.deltaTime in any way. Check it out.... Just change Time.timeScale without using deltaTime to multiply your velocity. The rigidbody.velocity objects will slow down for you.

    Although you're right, velocity actually uses Time.fixedDeltaTime, not Time.deltaTime. Still, the result will be the same. You'll make your velocity framerate dependent by multiplying the velocity by Time.deltaTime.

    You don't need to use FixedUpdate to change your velocity, either. Velocity automatically only runs on the fixed frames, regardless of when you change it.
     
  9. Rajdeep100

    Rajdeep100

    Joined:
    Sep 11, 2018
    Posts:
    9
    correct!!