Search Unity

Is there a Unity specific function to extrapolate a rigidbody's new position over time?

Discussion in 'Scripting' started by JtheSpaceC, Oct 28, 2014.

  1. JtheSpaceC

    JtheSpaceC

    Joined:
    Oct 5, 2014
    Posts:
    14
    I'm self teaching myself programming and Unity and sometimes it's a real detriment not to have a lecturer to ask, but I'm trying to work out a moving target's new position over time (2D in my case). For simplicity, let's say the time I want is 1 second.

    So a target's new position will be its present position, plus its heading(rigidbody2D.velocity) by its speed (rigidbody2D.velocity.magnitude) by the time (1 second). That's just based on my secondary school Physics knowledge. Simple enough.

    So, on paper, I'd draw a triangle, get the hypotaneuse length (velocity.sqrmagnitude) and make that equal to length x.x and length y.y (I mean "squared", how do I type that?..). Then I could work it out, on paper at least.

    Coding this process into c# seems a bit unwieldy to me, and I'm thinking Unity must have a simpler function that I'm just unaware of for doing this exact job. Calculating a new position after time of (x).

    Is there? Or was I on the right track all along?

    Thanks folks
    Kevin
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    391
    Code (csharp):
    1. Vector3 newPosition = transform.position + rigidbody.velocity*t;
    where t is the length of time.
    You are operating with 3 Vector3s - you can treat it like a basic 1D equation. Xf = Xi + v*t

    So in your example, lets use an object at Origin (0,0,0), with a velocity of (3,4,0).
    At t=1, position will be (3,4,0).

    At t=2, position will be (6,8,0).

    This assumes nothing like gravity or drag operates on the object during the timespan.
    I hope that was what you were asking. Let me know if not.

    Edit:I just realized this was in 2D. Same concept should apply.
     
    JtheSpaceC likes this.
unityunity