Problem With Mathf.Lerp

okay im trying to get my camera to lerp to an objects position…i move the camera using this code…gameObject.transform.Rotate(Vector3(recoil, 0, 0));…so my camera is moved now im trying to get it to lerp back to another objects position…using this code…

var cameraToLerp : GameObject;

var lerpTime : float = 1;

function Update () 
{

    this.gameObject.transform.rotation.x = Mathf.Lerp(this.gameObject.transform.rotation.x, cameraToLerp.transform.rotation.x, lerpTime);
}

and the camera doesnt move at all…the camera moves when i comment out the lerp command…but i dont know how to solve this and there are no tutorials that work.

All im trying to accomplish in this code is for the object to return back to the same rotation value as the other object...like the x,y,z rotation values....i need my object to return to those values over a certain period of time...im just having trouble finding the right tutorial for my problem.

–

nevermind i figured it out by putting time.deltatime for t...it worked just as i hoped...thanks anyways for trying to help me

–

2 Answers

2

Do you understand Mathf.Lerp? If something is not working, read the documentation.

You need to change the third parameter between 0 and 1 to change the value returned by Lerp.

im trying to figure out a way for my object to slowly return to another objects rotation...it doesnt matter if im using lerp or not....i just need help figuring this rotation problem out.

–

Going off of what Ben said, it seems you don’t really understand what Lerp does.

I’ll try my best to explain it, as I didn’t fully understand it until recently.

When people talk of Lerping something, they mean to move an object from point A to point B over t Time, where t DOES NOT equal the actual “arrival time.” t Will NEVER equal the arrival time, and will always be between 0 and 1. 0 is the start, and 1 is the end. (You need division here)

I’ll use “arrival time” a lot in my explanation, but just know that arrival time means the total time that you want the number to go from its’ start to its’ finish (or its’ 0 to 1)

I’ll post my gravity script as an example:

    IEnumerator Lerp_CurrentGravity()
    {
        _gravity.currentGravity = 0;

        float totalTimeDelta = 0;
        // Gradually Adjust Gravity
        while (_gravity.currentGravity < _gravity.maxGravity && GetGravityState() == Gravity.State.Falling)
        {
            totalTimeDelta += Time.deltaTime;
            _gravity.currentGravity = Mathf.Lerp(_gravity.currentGravity, _gravity.maxGravity, totalTimeDelta / _gravity.timeForMaxGravity);        // Lerps the gravity
            yield return new WaitForSeconds(Time.deltaTime);
        }

        if (GetGravityState() != Gravity.State.Falling)
            _gravity.currentGravity = 0;
    }

Look inside the While loop and you’ll see a totalTimeDelta float. That represents the total time that you’ve been “falling” in this instance of my script.

Now if you know that your actual “arrival” time is 2, then you’ll need to do the operation:

(totalTimeDelta / 2), which will give you an answer between 0 and 1.

This works for every sort of Lerping because its’ all based around the same principal.

WHAT YOU’RE DOING:

You’re setting the Lerp value directly to 1, which, is exactly the same as saying “Finish Lerping.” You might as well use transform.rotation = new Quaternion(x) at that point.