- Home /
How to make a smooth camera transition
Hello, my problem is the following.
I have a camera and 2 objects. The tag for one of the objects is "Player" and the other object is untagged. The camera will be looking at the object with tag "Player". But if you get close enough to the untagged object and press the C key, now the untagged object will be tagged as "Player" and the other object will now be untagged so the camera is now looking at the other object because it is now tagged as "Player" and the second object is now untagged.
The problem is when camera starts looking to the other object, this switch is really fast. I'd like it to be more smooth. Not looking at one object and the next frame already looking to the other. How can I achieve this?
Thanks!
Answer by swanne · Oct 16, 2021 at 09:23 AM
Hi @juancmaz
I use a basic camera follow script with smoothly follows the main player character. When the player dies and respawns the camera locates the player and moves smoothly to it. Perhaps you could adapt this to work with your player swap logic.
Here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offset;
private void FixedUpdate()
{
Vector3 desiredPosition = target.position + offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
transform.position = smoothedPosition;
transform.LookAt(target);
}
}
Hello! Thanks for your help. I will try it out and let you know if it works for me :)