1
votes

i want to be able to know the following positions when animating an object:

Last Frame transform.position
Current transform.position

i know how can i get the current transform.position just by asking transform.position in the LateUpdate(){} callback. but how can i get the last frame position?

Thanks!

1
Why don't you just store current transform.position so that when the next Update is called you can read and use this variable which will be last frame transform.position? - Unai
@Unai i am thinking about it... - or azran
There is no need to think - that is literally the way to do it. - Tom 'Blue' Piddock
it will work for me, thanks! - or azran

1 Answers

2
votes

This is the basic structure behind it:

class PositionUpdaterThing
{
    Vector3 _lastPosition;

    void Update() 
    {
        // get current position
        currentPosition = transform.position;

        // do anything you need to with the positions
        DoStuff(currentPosition, _lastPosition);

        // set last to current so the next frame of Update() is ready
        _lastPosition = transform.position;
    }

    void Start()
    {
        // set initial value for lastPosition
        _lastPosition = transform.position;
    }
}