camera Lerp Silly Question

having a C# moment…

My Lerp is Not Lerping over time despite Time.Deltatime

It just SNAPS

My Speed Variable cuts off the Lerp unless it’s like 100 (?) in my inspector

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lerp : MonoBehaviour {

    public  Vector3 startPos;
    public Vector3 endPos;
    public float speed = .8f;
    public float counter = 0;
    public float target = 10;



	// Update is called once per frame
	void Update () {
    }


    void OnMouseUp()
    {
        //record where the Camera is
        startPos = Camera.main.transform.position;
        //make it go forward 1.8 units
        endPos = Camera.main.transform.position + Camera.main.transform.forward * 1.8f;
        // Move over time
        Camera.main.transform.position = Vector3.Lerp(startPos, endPos, (speed*Time.deltaTime));
        // set an IEnumerator Coroutine to wait
        StartCoroutine(MyWait());
        //reset counter when com back from coroutine, so we can use again
        counter = 0;
        target = 10;

}




    IEnumerator MyWait()
    {
        // wait for seconds
        while (counter < target)
        {
            counter += Time.deltaTime; yield return null;
        }
        //put the Camera back to where it was
        Camera.main.transform.position = Vector3.Lerp(-endPos, startPos, (speed * Time.deltaTime));
    }



}

THANKS For Looking

~be

Try increasing your speed variable I don’t know exactly but I think if the Lerp is to low it will do what your describing, aka just snap into a position immediately.

You’re using Lerp incorrectly because you’re never incrementing the third parameter. This is a common problem and has bedn explained well before, e.g. at Using Vector3.Lerp() correctly in Unity — One Man's Trash is Another Man's Blog

@tanoshimi Thank you Tanoshimi,

You are right but I am still having problems;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lerp : MonoBehaviour {

    public  Vector3 startPos;
    public Vector3 endPos;
    public float speed = .8f;
    public float counter = 0;
    public float target = 10;
    public float timeTakenDuringLerp = 1f;
    public float distanceToMove = 1.8f;
    private float _timeStartedLerping;
    //Whether we are currently interpolating or not
    private bool _isLerping;
    private bool _isLerpingBack;


    // Update is called once per frame
    void Update () {
    }


    void OnMouseUp()
    {
        //record where the Camera is
        startPos = Camera.main.transform.position;
        //make it go forward 1.8 units
        endPos = Camera.main.transform.position + Camera.main.transform.forward * 1.8f;
        // Move over time
        _isLerping = true;
        // Camera.main.transform.position = Vector3.Lerp(startPos, endPos, (speed*Time.deltaTime));
        // set an IEnumerator Coroutine to wait
        StartCoroutine(MyWait());
        //reset counter when com back from coroutine, so we can use again

        counter = 0;
        target = 10;

}




    IEnumerator MyWait()
    {
        // wait for seconds
        while (counter < target)
        {
            counter += Time.deltaTime; yield return null;
        }
        //put the Camera back to where it was
        // Camera.main.transform.position = Vector3.Lerp(-endPos, startPos, (speed * Time.deltaTime));
        _isLerping = false;
        _isLerpingBack = true;
    }





     void FixedUpdate()
    {
        if (_isLerping)
        {
            //We want percentage = 0.0 when Time.time = _timeStartedLerping
            //and percentage = 1.0 when Time.time = _timeStartedLerping + timeTakenDuringLerp
            //In other words, we want to know what percentage of "timeTakenDuringLerp" the value
            //"Time.time - _timeStartedLerping" is.
            float timeSinceStarted = Time.time - _timeStartedLerping;
            float percentageComplete = timeSinceStarted / timeTakenDuringLerp;

            //Perform the actual lerping.  Notice that the first two parameters will always be the same
            //throughout a single lerp-processs (ie. they won't change until we hit the space-bar again
            //to start another lerp)
            Camera.main.transform.position = Vector3.Lerp(startPos, endPos, percentageComplete);

            //When we've completed the lerp, we set _isLerping to false
            if (percentageComplete >= 1.0f)
            {
                _isLerping = false;
            }
        }
        else if (_isLerpingBack)
        {
            float timeSinceStarted = Time.time - _timeStartedLerping;
            float percentageComplete = timeSinceStarted / timeTakenDuringLerp;

            Camera.main.transform.position = Vector3.Lerp(endPos, startPos, percentageComplete);

            //When we've completed the lerp, we set _isLerping to false
            if (percentageComplete >= 1.0f)
            {
                _isLerpingBack = false;
            }
        }
    }


}

Any ideas?

Thanks

~be