Move to position P for a duration of D

I have a function that I use to rotate my GameObject, found on an extremely helpful post on Unity Answers, here, posted by @Hellium.

I’ve updated it slightly to allow me to specify whether the rotation should be clockwise or anti-clockwise:

private void TurnPlayer( Vector3 direction )
{
    if( !Rotating )
    {
        if( direction == Vector3.left) // if left
        {
            StartCoroutine( Rotate( new Vector3( 0, -90, 0 ), 0.5f ) );
        }
        else if( direction == Vector3.right) // if right
        {
            StartCoroutine( Rotate( new Vector3( 0, 90, 0 ), 0.5f ) );
        }
    }
}

private IEnumerator Rotate( Vector3 angles, float duration )
{
    Rotating = true;
    Quaternion startRotation = transform.rotation;
    Quaternion endRotation = Quaternion.Euler( angles ) * startRotation;
    for( float t = 0; t < duration; t += Time.deltaTime )
    {
        transform.rotation = Quaternion.Lerp( startRotation, endRotation, t / duration );
        yield return null;
    }
    transform.rotation = endRotation;
    Rotating = false;
}

I also need a function to move a game object forward/backward in a similar manner, where i know the start position and end position, and smoothly transition to that end position. I guess if Quaternion.Lerp handles the rotation, i’m looking for the position equivalent of that.

How can I convert the code above to allow me to function that moves the GameObject forward / backward?

Thanks!

2 Answers

2

private IEnumerator Translate( Vector3 deltaPosition, float duration )
{
Translating = true;
Vector3 startPosition = transform.position;
Vector3 endPosition = startPosition + deltaPosition;
for( float t = 0; t < duration; t += Time.deltaTime )
{
transform.position = Vector3.Lerp( startPosition, endPosition, t / duration );
yield return null;
}
transform.position = endPosition;
Translating = false;
}

Of course, I was looking for Vector3.Lerp. Thanks very much (again) for your help!

–

Vector3.lerp is the equivalent that you are looking for. If you want someone to write the code for you, that isn’t really what Unity Answers is about. I can tell you however that this is how the Lerp function works.

You essentially have to give the function 2 Vector3s. The first one is the start point, the second one is your end point in 3D space. So for example:

transform.position = Vector3.lerp(startPointVector3, endPointVector3, timeValue);

Will set the position of the transform to the timeValue float. The timeValue float is always thought of as time, but sometimes it’s easier to think of it as a normalized range between the 2 vectors. So since the timeValue is normalized (from 0.0 - 1.0), halfway between the 2 vectors is 0.5f in terms of the timeValue.

So if you have a Vector3.lerp function with a startPoint of (500,500,500) and an end point of (0,0,0), a timeValue of 0.0f will return (500,500,500), a timeValue of 0.5f will return (250,250,250) and 1.0f will of course be the end point (0,0,0).

Now of course you will want to change that timeValue every frame to have a nonstop movement. So, you will just have to keep the start and end points the same in the lerp function and just change the timeValue. This is usually done with something along the lines of:

timeValue = timeValue + Time.deltaTime * speed;

With the speed being how fast you want this to happen. so If you start with timeValue as 0.0f and add it to itself plus the time passes since last frame (Time.deltaTime) and multiply that by how fast you want this to all happen, you get your your timeValue or point in the range between the start and end points…

Good luck

Thanks for taking the time to explain how Vector3.Lerp actually works - your explanation is better than the Unity documentation! Its alot to take in, and while Heelium's answer was what I was looking for in the short term (litterally a vector3 version of the function I already had), I'll definitely be investing time to understanding your explanation (and Lerp in general) much better. Thanks!

–