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
2
Question by TDGuy · Jun 09, 2012 at 03:46 AM · c#camerapositiongui-button

Script to move between camers

I'm trying to set up a script (C#) where when I click on a button it will go to another camera. Presently my knowledge in C# is very limited (call me a noob) so a little assistance as to where I can educate myself or some form of code to help me in the right direction.

Presently I have a camera on each of the objects that are going to be effected (instead of having the camera moving) which is how I want to do it, if that's in-effecient then I'm willing to work another route.

Because of my current knowledge and experience I am unsure where to start, I have looked at the scripting reference and it doesn't make any sense. In advance, thank you for your help.

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
2
Best Answer

Answer by aldonaletto · Jun 09, 2012 at 04:30 AM

This will be highly inefficient if all the cameras are enabled; if you keep all cameras disabled and only the current one enabled, there's no problem.
You could place all cameras in an array at the Inspector, and jump sequentially from one to another each time you click some button, like this (attached to any scene object):

using UnityEngine; using System.Collections;

public class SwitchCameras: MonoBehaviour {

 public Camera[] cameras; // set the size and drag the cameras here in the Inspector
 public int curCamera = 0;
 
 void Start(){
     SelCamera(curCamera);
 }

 void SelCamera(int n){
     for (int i = 0; i < cameras.Length; i++){
         cameras[i].enabled = (i == n); // enables only the camera index n
     }
 }
 
 void OnGUI(){
     if (GUI.Button(new Rect(10, 10, 100, 40), "Switch camera")){
         curCamera = (curCamera + 1)% cameras.Length;
         SelCamera(curCamera);
     }
 }

}

Comment
Add comment · Show 1 · 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
avatar image TDGuy · Jun 12, 2012 at 12:31 AM 0
Share

This is very helpful, I have to some tweeking with the script but it is a good start.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I get exact screen position of 3D game object in front of camera while using cardboard in VR game? 1 Answer

RTS Camera movement wrong after rotation 1 Answer

Getting camera to stick to object 1 Answer

How can I keep three targets in sight, and keep the camera above a specific height - while it resizes? 0 Answers

Camera Follow Player using a path 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