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. Dismiss Notice

Is it necessary to use Time.deltaTime for non-player controlled movement like projectile?

Discussion in 'Scripting' started by davidhfw, Sep 7, 2016.

  1. davidhfw

    davidhfw

    Joined:
    Aug 17, 2016
    Posts:
    77
    I have read that we need to use speed * Time.deltaTime to make player controlled movement speed independent of frame rate.

    Do we need to use Time.deltaTime to make the projectile speed independent of frame rate?

    I have experimented with and without Time.deltaTime.

    With the key pressed down (Input.GetKeyDown()) continuously to activate the firing of the projectile, the projectiles appear in cluster of 2.

    Projectile 1 fired
    Interval of x sec
    Projectile 2 fired
    Interval of (x + y) sec
    Projectile 3 fired
    Interval of x sec
    Projectile 4 fired
    Interval of (x+y) sec

    Without Time.deltaTime, the interval between projectiles seems to be more consistent.

    Is there some tweak required to make the projectile interval more consistent with Time.deltaTime?

    Thank you !
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Time.deltaTime just gives you the time since the last Update. You could handle all your movement and spawning every Update.. (or say every 3rd Update.. etc.). Say you moved your player every update, and you wanted him to move a certain distance in 60 Updates. On a machine running at 60 fps this would take 1 second. If my machine was only running your game at 30 fps it would take me 2 seconds to run that same distance.

    By using Time.deltaTime you can make sure your characters move a ceratain distance in 1 second regardless of frames.

    The same logic can be applied to firing projectiles. IF you just use update to fire the projectiles then 60 fps guy will fire x2 as many projectiles as 30fps guy.

    We'd need to see your code that fires projectiles to actually see whats going on with your firing sequence, and what you could expect to happen on to people running at differernt frame rates.
     
  3. Fab4

    Fab4

    Joined:
    Sep 30, 2012
    Posts:
    114
    Delta time gives you the actual time that has passed scince the last frame. Therfore it can be used to move objects (velocity multiplied by time= way)
    The problem is that frame rates are not consistent. So differnces between the frames are compensated by Time.deltaTime.
    As far As I understood your Problem correctly, you now want to fire projectiles in a periodic time.
    You should never rely on the framerate because people with high end machines would be able to fire twice as often when the reach a twice as high frame rate.
    In other words: You have to think about compensating too big time steps . What does that mean exactly?
    Example:
    You want to fire 60 projectiles each second.
    You have a framerate of exactly 60 fps.
    You can fire each frame one projectiles.
    Example 2:
    You want to fire 60 projectiles each second
    You have a framerate of exactly 30 fps
    You can See that you know were supposed to fire inbetween the frames.
    The only way I See herein, is to count the time when the last projectile was fired, and when you were supposed to fire the next one. In this example at 50% of your deltaTime.
    So you have to add the projectile from the time before, and change its Position to where it would actual be (projectile speed * (current time - time of actual fire) + start position)
    You also must check if there is a target between the start and current Position or your projectiles would fly through everything.
     
  4. davidhfw

    davidhfw

    Joined:
    Aug 17, 2016
    Posts:
    77
    With the space bar pressed down and doing a continuous fire, sometimes I see a slow moving projectile when I am using the code with delta.Time.


    Code (CSharp):
    1. void Update () {
    2.  
    3.         playerMovement(); //Method to detect key strokes for player movement
    4.         if (Input.GetKeyDown(KeyCode.Space))
    5.         {
    6.             InvokeRepeating("Fire", 0.001f, repeatRate);
    7.         }
    8.  
    9.         if (Input.GetKeyUp(KeyCode.Space))
    10.         {
    11.             CancelInvoke("Fire");
    12.         }
    13. }
    14.  
    15. private void Fire()
    16.     {
    17.  
    18.         GameObject projectile = Instantiate(laser.gameObject, transform.position, Quaternion.identity) as GameObject;
    19.      
    20.         projectile.GetComponent<Rigidbody2D>().velocity = Vector2.up * projectileSpeed * Time.deltaTime;
    21.  
    22. //Alternative code for the velocity
    23.         //projectile.GetComponent<Rigidbody2D>().velocity = Vector2.up * projectileSpeed;
    24.  
    25.     }

     
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    What is your projectileSpeed. You might just need to increase that. Time.deltaTime will usually be about 15-45 milliseconds. 0.015->0.045. if projectileSpeed is 1.0f then your gonna move 1 unit in 1 second. I'm not sure how much screen space a unit is.. but if your using an orthographic camera set to size 5, then your y-axis is 10 units long and it would take 10 seconds to move the entire screen.

    Just increase projectileSpeed til you like how fast the arrows are moving. Keeping your code frameRate independent is a good idea.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    You don't need to use Time.deltaTime when setting the velocity of a rigidbody, as you're doing. The velocity of a rigidbody is how fast it moves per second, so it handles the deltaTime stuff internally (in the physics engine).

    You need Time.deltaTime if you're moving a transform manually, or if you're adding force to a rigidbody.
     
    Fab4 likes this.