Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Araganor · Dec 05, 2012 at 08:14 PM · cameraoptimizationplanetary

More Effective Camera Switching

For a Video Game Design class project, I have been working on a simple solar system. It has all of the planets rotating around the the sun, etc. I wanted to create a GUI element where you could click on a button with the name of the planet (ex: button says "Earth"), and the camera would switch to follow that planet's orbit. I read up on the scripting reference, and I came up with something that worked after much forum browsing/trial and error (I am pretty new to scripting). I was wondering if someone could show me a more efficient way to do this. First, I'll show the way I came up with.

BasicalIy, my method was to make a camera for each planet, and attach this script to each:

 #pragma strict
 var target : Transform;
 
 
 function Update () {
     transform.LookAt(target);
 
 }

That one makes the camera look at its designated planet. The other script was for the GUI:

 #pragma strict
 
 var mainview : Camera;
 var marsview : Camera;
 var earthview : Camera;
 var mercuryview : Camera;
 var jupiterview : Camera;
 var plutoview : Camera;
 //states variables of the cameras
 
 function Start () {
     //deactivates all cameras but the main camera
     mainview.camera.active = true;
     marsview.camera.active = false;
     earthview.camera.active = false;
     mercuryview.camera.active = false;
     jupiterview.camera.active = false;
     
 }
 
 
 function OnGUI () {
     //creates GUI
     GUI.Box (Rect (0, 10, 200, 90), "Camera View Switch");
     if (GUI.Button (Rect (20, 40, 80, 20), "Earth")) { //first button
         mainview.camera.active = false;
         marsview.camera.active = false;
         mercuryview.camera.active = false;
         jupiterview.camera.active = false;
         earthview.camera.active = true;
         //activates earthview camera, deactivates all others
     }    
     if (GUI.Button (Rect (20, 70, 80, 20), "Mars")) {
            mainview.camera.active = false;
         earthview.camera.active = false;
         mercuryview.camera.active = false;
         jupiterview.camera.active = false;
         marsview.camera.active = true;
     }
     if (GUI.Button (Rect (20, 100, 80, 20), "Sun")) {
         earthview.camera.active = false;
         marsview.camera.active = false;
         mercuryview.camera.active = false;
         jupiterview.camera.active = false;
         mainview.camera.active = true;
     }
     if (GUI.Button (Rect (20, 130, 80, 20), "Mercury")) {
         earthview.camera.active = false;
         marsview.camera.active = false;
         mainview.camera.active = false;
         jupiterview.camera.active = false;
         mercuryview.camera.active = true;
     }
     if (GUI.Button (Rect (20, 160, 80, 20), "Jupiter")) {
         earthview.camera.active = false;
         marsview.camera.active = false;
         mainview.camera.active = false;
         mercuryview.camera.active = false;
         jupiterview.camera.active = true;
     }
     if (GUI.Button (Rect (20, 190, 80, 20), "Pluto")) {
         earthview.camera.active = false;
         marsview.camera.active = false;
         mainview.camera.active = false;
         mercuryview.camera.active = false;
         jupiterview.camera.active = false;
         plutoview.camera.active = true;
     }
 
 }
 
 function Update () {
 
 }

I didn't include all of the planets for the sake of example, but you get the idea. Being pretty new to this, I was glad I could find a solution that worked. Now I am looking for community input. If it isn't too much trouble, could someone show me a better, more efficient/effective way to accomplish this?

Comment
Add comment
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
1
Best Answer

Answer by AlucardJay · Dec 06, 2012 at 03:00 AM

You could just use an array for your cameras, and then loop through the array, given an index activate that camera, and disable all others in a loop :

 #pragma strict
 
 // Drop cameras in Inspector **IN ORDER**
 // eg Main; Sun; Mercury; Venus; Earth; etc etc
 
 var cameraView : Camera[];
 
 
 function OnGUI() 
 {
     if (GUI.Button (Rect (20, 40, 80, 20), "Earth"))
     {
         SetCamera( 4 ); // 5th item in list, 4th counting from zero
     }
     if (GUI.Button (Rect (20, 70, 80, 20), "Mars"))
     {
         SetCamera( 5 ); // 6th item in list, 5th counting from zero
     }
     if (GUI.Button (Rect (20, 100, 80, 20), "Sun"))
     {
         SetCamera( 1 ); // 2nd item in list, 1st counting from zero
     }
 }
 
 
 function SetCamera( index : int ) 
 {
     for ( var i : int = 0; i < cameraView.Length; i ++ )
     {
         cameraView[i].enabled = false;
     }
     
     cameraView[index].enabled = true;
 }
 
 
 // this could also be written like : 
 /*
 function SetCamera( index : int ) 
 {
     for ( var i : int = 0; i < cameraView.Length; i ++ )
     {
         if ( i == index )
         {
             cameraView[i].enabled = true;
         }
         else
         {
             cameraView[i].enabled = false;
         }
     }
 }
 */
Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Rotating an object to face the camera regardless of the cameras position. 3 Answers

Smooth camera follow return 1 Answer

Unassigned Reference Exception 4 Answers

How to render severals cameras at differents Frame Rate ? 2 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