Vector3.Lerp() not working correctly

I am trying to make 3 different position where the camera can move between. Everything works fine EXCEPT Vector3.Lerp (). Like, the camera moves, but it BARELY moves. Like literally, it moves on the X axis by 0.02 something or close. Here is the full script.

using UnityEngine;
using System.Collections;

public class CameraLookOver : MonoBehaviour
{
    //Transform Variables
    public Transform playerTransform;
    private Vector3[] newCameraPosition = new Vector3[3]; //[0]: Left Camera Position, [1]: Center Camera Position, [2]: Right Camera Position

    //Int Variables
    public int currentCameraPosition = 0;

    void Start ()
    {
        newCameraPosition[0] = new Vector3 (-1.25f, 3.0f, -3.5f);
        newCameraPosition[1] = new Vector3 (0.0f, 3.0f, -3.5f);
        newCameraPosition[2] = new Vector3 (1.25f, 3.0f, -3.5f);

        this.transform.position = new Vector3 (playerTransform.position.x, playerTransform.position.y + 1.9f, playerTransform.position.z - 3.5f);
        this.transform.rotation = Quaternion.Euler (new Vector3 (15.0f, 0.0f, 0.0f));
    }

    void Update ()
    {
        if (currentCameraPosition > 2)
            currentCameraPosition = 0;

        if (Input.GetKeyDown (KeyCode.P))
        {
            this.transform.position = Vector3.Lerp (transform.position, newCameraPosition[currentCameraPosition], Time.deltaTime);
            currentCameraPosition++;
        }
    }
}

The 3 input value in lerp is supposed to go from 0 to 1. Time.deltaTime is the time between each frame and stays almost constant (depending on your framerate).

1 Like

Lerp needs to be 0-1, as said above

float lerpVal = 0;

void Update(){
lerpVal += time.DeltaTime;
//Stay between 0 and 1
lerpVal = Mathf.Clamp(lerpVal,0,1);
//Lerp at lerpVal


}
val = Vector3.Lerp (val, targetVal, Time.deltaTime);

Is a fairly common smoothing operation, there isn’t anything wrong with that. Every frame it gets closer to the target position, slowing down.

The problem is that the lerp is only performing on the few frames that the P key is held down.

Ah yes I just realized that too.

@Op use Input.GetKey(KeyCode.P) if you want to do continuous input. If you want to have it pressed once store a boolean value for lerping, then say if(Lerping), etc.

Got it working like this. Everything works as I want it to now, but Unity gives me an array index error because one of the variable’s is 3 for less than a split second of a split second of a split second.

Here is the updated code

using UnityEngine;
using System.Collections;

public class CameraLookOver : MonoBehaviour
{
    //Transform Variables
    public Transform playerTransform;
    public Transform[] newCameraPosition = new Transform[3]; //[0]: Left Camera Position, [1]: Center Camera Position, [2]: Right Camera Position

    //Int Variables
    public int currentCameraPosition = 1;

    //Float Variables
    public float lerpSpeed = 5.0f;

    //Bool Variables
    public bool isLerping = false;

    void Start ()
    {
        this.transform.position = new Vector3 (playerTransform.position.x, playerTransform.position.y + 1.9f, playerTransform.position.z - 3.5f);
        this.transform.rotation = Quaternion.Euler (new Vector3 (15.0f, 0.0f, 0.0f));
    }

    void Update ()
    {
        //Resets the current camera position back to it's original position
        if (currentCameraPosition > 2)
            currentCameraPosition = 0;

        if (Input.GetKeyDown (KeyCode.P))
        {
            currentCameraPosition++;
        }

        this.transform.position = Vector3.Lerp (this.transform.position, newCameraPosition[currentCameraPosition].position, Time.deltaTime * lerpSpeed);
    }
}

The title of this thread is ludicrous; please have enough respect for Unity Tech to assume your own code is the problem, and prove it is not, before suggesting that one of the oldest functions in the API is buggy.

1 Like
    void Update ()
    {
        //Resets the current camera position back to it's original position

        if (Input.GetKeyDown (KeyCode.P))
        {
            currentCameraPosition++;
        }
        if (currentCameraPosition > 2)
            currentCameraPosition = 0;

        this.transform.position = Vector3.Lerp (this.transform.position, newCameraPosition[currentCameraPosition].position, Time.deltaTime * lerpSpeed);
    }

I got it fixed now. Everything is fine except one MORE thing. When the “currentCameraPosition” is on 2 and then it got reset to 0, the camera like zooms in for a split second and then goes back to where it is supposed to go. I went in the scene when the game was being played and I saw that when “currentCameraPosition” was reset back to 0, the Z axis on the camera moved to like 3.243453543 and then started heading toward 3.5 like it was supposed to. Any suggestions for this?

@Jessy I never mentioned that this Vector3.Lerp () was buggy, it was my code and I get that. But I will remember to make my title’s more clear from now on.