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

Laptop power affecting gameplay?

Discussion in 'Scripting' started by Simpso, Jun 21, 2017.

  1. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    [BELOW SOLVED]
    Took out the * time.deltatime and reduced the forecepower. All working great now.
    No idea why i added time.deltatime in there as i did.
    Must have picked it up from a tutorial or something.

    Hi guys,
    Ive noticed something really weird going on with a game im working on.
    Im working on a flappy bird clone.
    So if you know the game basically the sprite moves forward and jumps when a button is clicked.

    My issues it all this works find when my laptop is powered into the wall.
    When i unplug the laptop and it runs on battery the jump suddenly has more power to it.
    Got the same outcome when i moved the game over to my android phone.
    The jump power felt like it was twice as powerful.

    Anyone got any ideas what might be causing this?

    Could it be something to do with time.deltatime? FPS bounces between 60 and 70 when plugged in. Drops to 30 when unplugged.
    Thanks guys

    This sits under Update
    Code (csharp):
    1.  
    2.         if (Input.GetMouseButtonDown(0))
    3.         {
    4.             float jumppower = 300f;
    5.             rb2d.velocity = new Vector2(rb2d.velocity.x,jumppower * Time.deltaTime);
    6.         }
    7.  
     
    Last edited: Jun 21, 2017
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Strange, you use Time.deltaTime to have a more consistent movement over the same period of time no matter the frame rate, so the fact you feel the opposite is extremely strange.

    MID TYPE EDIT:
    Yes, this actually makes sense what youre experiencing. If the frame you press the left mouse button suddenly takes longer to render than the force would be higher. This is in fact an incorrect usage of Time.deltaTime.

    I will keep my original message so people can see my train of thought.
     
  3. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    So in general, i always thought you used time.deltatime to keeps things smooth as you say.

    The jump force works perfect now but i'm noticing slight "jumpyness" with the background scrolling which also uses time.deltatime.
    Is there an alternative to create smoothnes that wouldnt be affected by FPS?
     
  4. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    You could potentially look at AddForce or just remove Time.deltaTime.
     
  5. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    Took the time.deltatime part out but still getting the same result where the speed of movemoment slows when i pull the power out to the laptop.
    You think it could all be just tied to a slower FPS.
    The code i am using is simply:

    transform.Translate (Vector2.right * speed);
     
  6. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    You only want to use deltaTime when you want to do some continuous action. Usually applying a force or velocity is a one-time action, so it doesn't make sense to use deltaTime there.

    For example:

    You want your starting velocity to be in meters per second. Deltatime doesn't apply here.

    For every frame afterwards, you calculate your current distance given your starting position and velocity. Deltatime makes sense here.
     
    Simpso likes this.
  7. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    This is because you're using Update; update runs on every frame.

    When you have your laptop unplugged, your OS tries to save on battery life and will (by default) take aggressive steps to lower performance to squeeze out more battery time. In my experience it will throttle the CPU and GPUs clock speeds, among other things. In Windows 10, you can look at the power plans by going to Settings app > System > Power and Sleep > Additional Power Settings. Or you can go to Control Panel, Hardware and Sound, Power Options. Also, if you click the battery icon in the taskbar, there's a battery saver button on Windows 10 laptops.

    MacOS probably does the same thing too, but I don't own anything from Apple so I don't know the exact setting there.

    Since you're dealing with Rigidbody, you should probably do that in FixedUpdate. If you're doing Transform.Translate, you can use Unity's example:
    Code (CSharp):
    1. transform.Translate(Vector3.forward * Time.deltaTime);
    2. transform.Translate(Vector3.up * Time.deltaTime, Space.World);
     
    Last edited: Jun 23, 2017
    TaleOf4Gamers and Simpso like this.
  8. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    So just to clarify, i should only really be using Time.deltatime within Fixed update?
    This would keep the "timing" of things consistent regardless of the shifts in FPS caused by the device the game is played on. Is my understanding right here?

    Thanks
     
    TaleOf4Gamers likes this.
  9. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    You should only be using Time.deltaTime in Update. There is no purpose to be using Time.deltaTime in FixedUpdate since FixedUpdate runs on FixedTimeSteps, so the timing between each activation of FixedUpdate is constant.

    Using Time.deltaTime in Update however only applies for stuff you want happening on every frame, like a timer, although there are far better ways to make a timer.
     
  10. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    Oh right. My confusion seems to be when to use deltatime then.
    I currently have the player and background scrolling tot he right at the same speed.
    When i was using deltatime in my code with translate..... * speed * time.deltatime the speed of the game was beign determined by the FPS dependandt of the device.
    So for example, On my pc.. solid 60fps on my phone stuttery 30-40 fps.
    Took the time.deltatime out and the game became smooth.
     
  11. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    If you use Update without time.deltaTime you'll notice that objects moved by Update will change their speed based on your FPS. That's not very good, so it's probably best to simply learn how to use time.deltaTime.