Search Unity

Get the position of where an object was one second ago?

Discussion in 'Scripting' started by mctricks, Dec 1, 2010.

  1. mctricks

    mctricks

    Joined:
    May 25, 2009
    Posts:
    151
    Basically the title... I want to get the position of where a gameobject was one second ago. Is there a straight-forward way of doing that or is it a more complex deal?

    I tried searching google/this forum but I couldn't find anything
     
  2. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    If the object is moving predictably (e.g. at a constant speed in a straight line), you can calculate where it was. If its movement is not predictable in a way that would allow that, the only solution is to record it's position over time. Exactly how to do that would depend on how it's moving, how accurate you need the tracking to be, and so forth. Can you explain the higher level problem you're trying to solve (i.e. why you need to know where the object was at a point in the past)?
     
  3. hallamasch

    hallamasch

    Joined:
    Nov 29, 2010
    Posts:
    153
    A simple solution would be to, save the position every second.

    Pseudo Code
    Code (csharp):
    1.  
    2.  
    3. if timesup
    4. lastPosition = position
    5.  
    6.  
    Now there is many optimizations you can apply to that.
    For example if the object has not moved, don't set a newPosition.
     
  4. mctricks

    mctricks

    Joined:
    May 25, 2009
    Posts:
    151
    Well basically I need an object to chase the player in his wake... so I think I'll make an array of 10 vectors, and use it as the record of the player's position every 10th of a second... then use Vector3.Lerp to move the object fluidly
     
  5. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    Yes, if you need object A to follow the exact path object B took, you'll need to incrementally record that path. Depending on how closely object A is following, though, it may be sufficient to just move towards B without caring were B used to be. Of course that doesn't work if A is following at a distance; then it's back to recording B's path for A to reference.