Change Camera Position when pressing a button whilst changing scenes
Hi Everyone, I hope you all are doing well. I am still a beginner in Unity. I was hoping if someone could help me figure out how to change my camera locations on a press of a UI button. I am trying to create different scenes in which each scene will have different camera locations. A perfect example of what I am doing is the camera movement (with transition) that occurs in different scenes in SketchUp. For example, in the first scene, the camera points to a subject and there would be a dialogue with a UI button name "next". When I click that button I would like to change my scene and change my camera to a different location. I hope this makes sense. I really would appreciate it if someone could help me with this. Thank you!
Answer by tadadosi · Jun 20, 2020 at 03:40 PM
I just wrote this class that has the method SwitchToScene(int index)
to move a camera from its current position to a point stored on an array and after that action is done it will load a scene using the provided index.
Things that you need to take into account:
The scenes should be added in File -> Build Settings...
nextSceneCameraPoints should have the same amount of elements as the Build Settings scenes.
If you don't how to use an UI Button, a simple search could give you an idea.
The only thing that you need to add to your Button OnClick event is the method SwitchToScene and set the index of the scene that you would like to load when that button is pressed.
Make sure that the end point of the current camera matches the starting point of the camera in the next scene (to make a smooth transition).
You could also have a persistent camera by writting
DontDestroyOnLoad(gameObject);
on the awake method. It isn't what I had in mind when I wrote this class, but you could mess around with the idea to see how it goes.
Code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class MoveCamAndChangeScene : MonoBehaviour
{
public Camera myCamera;
[Header("Add points that represent the position that your next cameras should have.")]
public Transform[] nextSceneCameraPoints;
[Header("How much should the movement of the camera last?")]
public float lerpDuration;
[Header("Draw any curve you like to smooth the movement of your camera.")]
public AnimationCurve smoothingCurve;
private Vector3 startPosition;
private Quaternion startRotation;
private float t;
private int nextSceneIndex;
private int nextCameraPointIndex;
private bool isActive;
private void Awake()
{
CheckIfReady();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
// Use this public method on a UI button and you should be able to change
// move the camera towards the next cameraPoint and after that movement is
// done it will load the scene with the nextSceneIndex.
// Make sure that you added your scenes in File -> Build Settings...
SwitchToScene(0);
}
if (isActive)
{
// To convert our time into a number that goes from 0 to 1 and takes lerpDuration to get to 1.
t += Time.deltaTime / lerpDuration;
// New float to store the resulting t after using the smoothing curve.
float smoothTime = smoothingCurve.Evaluate(t);
// To make sure that our index is not out of range and that we actually got a point on that index.
if (nextCameraPointIndex <= nextSceneCameraPoints.Length - 1 && nextSceneCameraPoints[nextCameraPointIndex] != null)
{
transform.position = Vector3.Lerp(startPosition, nextSceneCameraPoints[nextCameraPointIndex].position, smoothTime);
transform.rotation = Quaternion.Lerp(startRotation, nextSceneCameraPoints[nextCameraPointIndex].rotation, smoothTime);
}
if (t >= 1)
{
isActive = false;
t = 0.0f;
// Check if our nextSceneIndex doesn't go out or range and
// as soon as the camera stops, load the next scene.
if (nextSceneIndex <= SceneManager.sceneCount - 1)
{
SceneManager.LoadSceneAsync(nextSceneIndex);
}
}
}
}
public void SwitchToScene(int index)
{
if (!isActive)
{
nextSceneIndex = nextCameraPointIndex = index;
isActive = true;
startPosition = transform.position;
startRotation = transform.rotation;
}
}
private void CheckIfReady()
{
if (nextSceneCameraPoints.Length == 0 || myCamera == null)
{
Debug.LogError(gameObject.name + ": Missing myCamera or camera points!");
Debug.Break();
}
}
}
You can also test this class right away, without the need of an UI Button, I set it to use the spacebar to switch to a scene, just follow the above things and change the method index if you want to go to a different scene. Best of luck!
Your answer
Follow this Question
Related Questions
UI Elements repositioning and resizing when playing game 1 Answer
Buttons dont work with a second Camera 0 Answers
How to switch cameras with ui buttons? 0 Answers
How to write a script for an existing button 1 Answer
Unity2D: How to destroy spawned object once it exit out of camera's view? 1 Answer