I'm using TRANSLATE but want to use LERP. C#

Hi everyone,

I’m working on a project that’s going to require a lot of fluid motion. There are going to be documents on a table that will need to move toward the camera when clicked. As of right now, I only have one problem: using a lerp instead of a translate to move my camera.

Here is what is working now:

	void TransitionCamera(){
		Scene.transform.Translate (Vector3.down * 100);
		selectionComplete = true;
	}
	
	void TransitionCameraBack(){
		Scene.transform.Translate (Vector3.up * 100);
		displayScore = true;
	}

Works fine to jump the Scene (including the main camera and the scene background) down and back up again. But I don’t want the Scene to jump, I want it to move smoothly over a few seconds. This will make it seem as if objects in the scene are floating upward, and then floating back into the scene when TransitionCameraBack is called.

Can anyone help me? I’ve tried implementing the Unity documentation on Vector3.Lerp. It didn’t help to smooth the transition, I got some errors, I lost a lot of sweat and time, and then I reverted to this original script out of fear.

Thank you a billion for taking the time to read!

You could try this:

        IEnumerator TransitionCamera()
        {
                var t = 0f;
                var start = Scene.transform.position;
                
                while(t < 1)
                {
                         t += Time.deltaTime;
                         Scene.transform.position = Vector3.Lerp(start, start + Vector3.down * 100, t);
                         yield return null;


                }
                selectionComplete = true;
        }

Then call it using:

         StartCoroutine(TransitionCamera());

I don’t use it that much myself, but i should mention it:

iTween

which is a free library and offers tons of lerping and tweening functions and is very easy to use. In most cases i implement simple tweens like whydoidoit showed in his answer because you have more control :wink: