0
\$\begingroup\$

So I want to make a smooth transition with the camera from point A to point B while keeping direction and rotation.

My current approach is using Vector3.Lerp :

using UnityEngine;
 
public class CameraMoveVertical : MonoBehaviour
{
    public float cameraSpeed = 0.125f;
 
    public void moveCamera()
    {
        Vector3 lerpPosition = Vector3.Lerp(transform.position, new Vector3(transform.position.x, transform.position.y+50, transform.position.z), cameraSpeed);
        transform.position = lerpPosition;
    }
}

The camera does move but the movement is instant. Any way to make it a smooth transition?

\$\endgroup\$
1
  • \$\begingroup\$ Did my answer resolve your question? \$\endgroup\$
    – Kevin
    Dec 28 '20 at 22:05
0
\$\begingroup\$

That script by itself won't do anything, since the moveCamera() function is never called. Are you calling moveCamera from another script?

Use a coroutine:

private IEnumerator MoveUp(float distance, float duration) {
    Vector3 startPosition = transform.position;
    Vector3 targetPosition = startPosition + new Vector3(0, distance, 0);
    float elapsed = 0;
    while (elapsed < duration) {
        yield return null; //wait until the next frame
        elapsed += Time.deltaTime;
        transform.position = Vector3.Lerp(startPosition, targetPosition, elapsed / duration);
    }
}

public void MoveUp() {
    StartCoroutine(MoveUp(50, 3));
}

A coroutine is a function that executes over time; you can use loops within a coroutine to do many kinds of animation, such as this simple position tween. Coroutines are a critical concept in Unity and you should take the time to learn them thoroughly if you're not already familiar with the topic.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .