Camera lerp not working

Hello. Hours of stumped. C# script… I have a working code that detects mouse swipes (thanks to the person who posted it here… )… and when the mouse swipe is detected I want my camera to move forward nice and smooth. Getting it to jump forward with each swipe is no problem. But even with the following code, it won’t move forward smoothly. Any help greatly appreciated…


if(mouse detect code goes here)
{
mainCamera.transform.position = Vector3.Lerp{transform.position, forwardTarget.position, Time.deltaTime *8);

Debug.Log("the mouse detect works because I see this");

this is attached to the main camera. And I have one public variable “forwardTarget” which I drop a cube into as the target. Ideally it would not even need a target, just pick a point x units in front of it, and lerp to that each time mouse is swiped…

Hopefully this is clear. Thanks

The value you’re passing to Lerp doesn’t make sense for what you’re trying to do.

Lerp takes three parameters:

  • from, your start position
  • to, your end position
  • t, a parameter indicating a percentage between from and to

If you consistently pass in the same t, you’ll consistently get the same result.

If you only call lerp once, you’ll only get one output.

If you want the output to change over time, you must call lerp multiple times with input that changes over time. Loops and counters are very useful, here.

Looking at your code: Time.deltaTime tells you how many seconds have passed since the last frame. If your game runs at 50 frames per second, it’s generally going to have a deltaTime around 0.02 every frame. In that case, you’d be passing roughly 0.16 to lerp during every frame.

What you probably want, instead, is to pass a number to lerp which gradually increases. For example, something like this:

public class Example : MonoBehaviour {
    float moveTime = 1f;
    
    Vector3 startPos;
    Vector3 endPos;
    float currentMoveTime;
    bool isMoving = false;

    void Update() {

        //CASE 1: camera is currently moving
        //  - advance timer
        //  - calculate percentage
        //  - apply movement
        if (isMoving) {
            currentMoveTime += Time.deltaTime;
            float perc = currentMoveTime / moveTime; //express as percentage (0 to 1)
            
            //check: is timer complete?
            if (currentMoveTime > moveTime) {
                perc = 1f;
                isMoving = false;
            }
            mainCamera.transform.position = Vector3.Lerp(startPos, endPos, perc);
            
        //CASE 2: check if camera *should* move
        //  - set movement start/end values
        //  - start moving
        } else if (**MOUSE DETECTION CODE**) {
            currentMoveTime = 0f;
            startPos = mainCamera.transform.position;
            endPos = mainCamera.transform.position + mainCamera.transform.forward * 10f;
            isMoving = true;
        }
    }
}

Thanks for the response… what you are saying makes sense, though I thought the third variable in a lerp statement did count from 0-1 on its own… I tried integrating your code, and it worked… but still there was no interpolation. it still jumps… I also tried rearranging your code just to see what would happen, and I get results but all the movements are still jumps… Here is the entire code, with your code added. This is attached to the main camera.If you drag it onto a camera you’ll see it instantly. Thanks for the help.!! If you have any other ideas be glad to hear them.

=====================

public class SwipeManager_zoomCamera2 : MonoBehaviour {


		//inside class
		Vector2 firstPressPos;
		Vector2 secondPressPos;
		Vector2 currentSwipe;
		
		public Camera mainCamera;
	        public Transform forwardTarget;
	        float moveTime = 1f;  //new
	        Vector3 startPos;
	        Vector3 endPos;
	        float currentMoveTime;
	        bool isMoving = false;

		
void Update () {
			
			
			if(Input.GetMouseButtonDown(0))
			{
				
				//save began touch 2d point
				firstPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
				
			} // end of if
			if(Input.GetMouseButtonUp(0))
			{
				
				//save ended touch 2d point
				secondPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
				
				//create vector from the two points
				currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y); 
				
				//normalize the 2d vector
				currentSwipe.Normalize();

//swipe upwards ----
//new
			//CASE 1: camera is currently moving
			//  - advance timer
			//  - calculate percentage
			//  - apply movement
			if (isMoving) {
				currentMoveTime += Time.deltaTime;
				float perc = currentMoveTime / moveTime; //express as percentage (0 to 1)
				
				//check: is timer complete?
			    if (currentMoveTime > moveTime) {
					perc = 1f;
					isMoving = false;
				}
				mainCamera.transform.position = Vector3.Lerp(startPos, endPos, perc *50);

				//CASE 2: check if camera *should* move
				//  - set movement start/end values
				//  - start moving
			} else if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
				currentMoveTime = 0f;
				startPos = mainCamera.transform.position;
				endPos = mainCamera.transform.position + mainCamera.transform.forward * 10f;
				isMoving = true;
			}



//old ------------------------				
/*
				//THIS IS WHERE I WANT THE CAMERA TO MOVE FORWARD-------------------------------
				if(currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
				{


				mainCamera.transform.position = Vector3.Lerp(transform.position,forwardTarget.position,Time.deltaTime *8);
				Debug.Log("up swipe");	


				}
*/				
//swipe down ------------------------------------
				if(currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
				{
					Debug.Log("down swipe");
					mainCamera.transform.Translate(Vector3.back * Time.deltaTime * 100,Space.Self );
				}

//swipe left ------------------------------------
				if(currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
				{
					Debug.Log("left swipe");
					mainCamera.transform.Translate(Vector3.left * Time.deltaTime * 50,Space.Self );
				}

//swipe right -----------------------------------
				if(currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
				{
					Debug.Log("right swipe");
					mainCamera.transform.Translate(Vector3.right * Time.deltaTime * 50,Space.Self );
				}
				
				
			} // end of if

		}  // end of voidUpdate
		

	}  // END OF CLASS
=====================