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. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,478
    Ask this in the correct forum (Scripting) and I'll give you a great answer, unless somebody beats me to it first. :)
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    If the velocity is constant.
    Code (csharp):
    1. Vector3 futurePosition = rigidbody.position + timeInSeconds * rigidbody.velocity;
    If the velocity is changing... well then that equation will depend entirely on how the velocity is changing.

    EDIT: For rigidbody2D, I think it's this:
    Code (csharp):
    1. Vector2 futurePosition = rigidbody2D.position + timeInSeconds * rigidbody2D.velocity;
    I checked the docs, everything is already in Vector2 not Vector3.
    http://docs.unity3d.com/ScriptReference/Rigidbody2D.html
     
    Last edited: Oct 28, 2014
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,478
    OK, no worries, here we are in the Scripting topic now. :)

    So yeah, this is pretty easy to do with vector math. Think of a vector like an arrow with a certain magnitude (length) and direction. If we assume the base of the arrow is at the origin (0,0,0), then this arrow specifies a point in 3D space. If we assume the base is somewhere else, then the arrow specifies a change from that point to some new point.

    Now, you can add vectors — that's the same as following one arrow, then positioning the second arrow with its base at the end of the first, and following the second arrow. As numbers, you just add up the X, Y, and Z components.

    You can also multiply vectors; this simply scales the length of the arrow. Multiply by 0.5, and your arrow is half as long as it was. In numbers, you just multiply X, Y, and Z.

    OK, so now you understand vector math! You wrote:

    Yep. Except you don't need to separate heading and speed out; just use velocity, which is both heading and speed together. (Velocity is change in position per unit of time — or in other words, it's the arrow that points from something's current position, to its new position 1 second later.) In code, this would be:

    Code (CSharp):
    1. Vector3 newPos = target.transform.position + velocity;
    That's assuming you really do want to know where it will be 1 second later... if you want some other time, then just multiply (scale) velocity by whatever time you want.

    Now, I've done this in 3D; in 2D it would be much the same, except you may have to cast target.transform.position to a Vector2 (I haven't done a whole lot of 2D stuff in Unity, so I'm not sure what implicit converters are there for that). But give it a try, and let us know if you run into trouble!
     
    GarthSmith likes this.
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Horray for Vector Math!
     
  6. JtheSpaceC

    JtheSpaceC

    Joined:
    Oct 5, 2014
    Posts:
    14
    Thanks for the help there guys. That multiplication is way simpler. I wasn't fully aware of how the .velocity worked.
     
    GarthSmith likes this.
unityunity