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

Calculate speed needed to get from one point to another in exactly 1 second.

Discussion in 'Scripting' started by Kenneth4848, Mar 19, 2018.

  1. Kenneth4848

    Kenneth4848

    Joined:
    Sep 14, 2017
    Posts:
    14
    How can I do this?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    speed = distance / time
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    velocity = distance / time
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Take two positions, PStart and PEnd.
    Take the difference PEnd - PStart and you'll get POffset.
    Take POffset's magnitude (which is the distance): that's your base value to combine with any further timing...

    Now, any further logic depends on what you wanna do with it.

    In theory, you'll need the constant speed of distance units to reach your target position in 1 sec.
    If you move it with that speed, you'll be moving 3600*distance units per hour.
    You could also scale the value to anything you'd like. If you want to move the distance in 1 hour, you'd move with distance/3600 units per second instead.

    Another way to look at it:
    Suppose your game runs at exactly X FPS, and you wanna reach the target in 1 Sec: you need to move at a speed with distance/X units per frame, i.e. if you were running the game at constant 60 FPS, you'd have a speed of 0.1666666... units / frame (in this scenario a frame takes = 1/X seconds).

    However, in practice that doesn't help alot, because you're usually not running at an ideal constant frame rate. So, you'd use the distance as speed per X seconds and move your objects at distance/X and multiply that value with Time.deltaTime, in order to compensate the differences in frame time.
     
    Last edited: Mar 19, 2018
  5. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
  6. Kenneth4848

    Kenneth4848

    Joined:
    Sep 14, 2017
    Posts:
    14