• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by vik4christy · Jun 20, 2020 at 03:02 AM · camerauibuttonobjectbeginner

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 w$$anonymous$$ch 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 t$$anonymous$$s makes sense. I really would appreciate it if someone could help me with t$$anonymous$$s. Thank you!

@maccabbe @tadadosi

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by tadadosi · Jun 20, 2020 at 03:40 PM

I just wrote t$$anonymous$$s 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.


T$$anonymous$$ngs 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 t$$anonymous$$ng 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 t$$anonymous$$s 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 smoot$$anonymous$$ngCurve;
 
     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 t$$anonymous$$s 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 smoot$$anonymous$$ng curve.
             float smoothTime = smoot$$anonymous$$ngCurve.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 t$$anonymous$$s 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 t$$anonymous$$ngs and change the method index if you want to go to a different scene. Best of luck!

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

367 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

UI Elements repositioning and resizing when playing game 1 Answer

Vuforia - UI that overlays on top of ARCamera 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges