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

Reliable way to move a GameObject at a steady rate?

Discussion in 'Scripting' started by Punfish, Jan 1, 2015.

  1. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    I suspect this is a very simple resolution to this but I haven't yet found it.

    I'm using this very simple code piece...
    Code (csharp):
    1.  
    2. _move.Speed = MoveSpeed * Time.deltaTime;
    3. transform.position = Vector2.MoveTowards(playerPosition, movePosition, _move.Speed);
    4.  
    And it's being called every Update (I've also tried FixedUpdate). However, when the FPS rate spikes or drops the movement happens at a much faster, noticeable rate. I'm just trying to keep a steady speed.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    p1 = p0 + velocity * time

    So, if your object is moving at speed MoveSpeed, multiply by Time.deltaTime, as you do. Then add that to the current position:

    Code (csharp):
    1. transform.position += _move.Speed;
     
  3. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    I'm sorry, I do not understand. transform.position expects a Vector3/2 while _move.Speed is a float containing value 30 in my case.
     
  4. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Ah okay. So what direction does the GO travel in? If you know the direction it travels in, then multiple that direction by your speed.
     
    Magiichan likes this.
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You have to define a direction for the Object to travel to, and multiply that direction with the Time.deltaTime variable, this makes it travel at the same speed, regardless of your framerate.

    Code (JavaScript):
    1. function Update() {
    2.      var direction : Vector2 = Vector2.right;
    3.      Transform.position += direction * Time.deltaTime;
    4. }
    Code (CSharp):
    1. void Update() {
    2.      Vector2 direction = Vector2.right;
    3.      Transform.position += direction * Time.deltaTime;
    4. }
    in your case you should do this
    Code (CSharp):
    1. var step : float = MoveSpeed * Time.deltaTime;
    2. var newPosition : Vector2 = Vector2.MoveTowards(currentPosition, targetPosition, step);
    Code (CSharp):
    1. float step = MoveSpeed * Time.deltaTime;
    2. Vector2 newPosition = Vector2.MoveTowards(currentPosition, targetPosition, step);
    It seems that you're already doing this correctly though...
    What exactly are you experiencing?

    By the way, make sure that this code is inside the update function!
     
    Punfish likes this.
  6. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    That is what I'm doing. It just seems that on occasion my object will move a bit faster on occasions. Others notice it as well. Maybe I'm dropping frames somewhere. The artifact doesn't happen often so I'll shrug it aside for now and see if it's an issue at compile time.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    The point of Time.deltaTime is that it uses the time since the last frame. So dropping frames doesn't affect movement speed.

    --Eric