Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 evil_debellator · Aug 10, 2015 at 05:20 AM · cameratoggle buttoncamera controlscamera scriptswitch cameras

Making a camera switch in JS, how do I add more cameras?

So, here is my set-up... I have two cameras in my scene with the Tag "MainCamera" .

One camera is a third person camera with a script of its own, it targets to the main player. It has settings for height, distance and for rotating around the player.

The other Camera is a first person camera, it 90% same code as the first camera except that some settings such as height and distance are fixed and moving the mouse on the horizontal axis rotates the player. The first person camera is set up so that it places itself inside the player's head when it is active.

I made a script and an empty object to place the script.

I want to press the key 'V' to switch between the two cameras and possibly add more cameras that may be toggled with the script.

How do I add more than two cameras?

Here is the code-

 #pragma strict
 
 var cam1 : GameObject;
 var cam2 : GameObject;
 //var Camera3 : GameObject;
 
 var camOn : boolean = false;
 
 
 function Start () {
 
 }
 
 function Update () {
     
     if(Input.GetKeyDown(KeyCode.V)) {
          camOn = !camOn;
      if(camOn){
     cam1.active = true;
      cam2.active = false; }
      if(!camOn){
         cam2.active = true;
      cam1.active = false; }
      }
 }
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

Answer by KdRWaylander · Aug 10, 2015 at 07:17 AM

Hi :)

You want to have a list of all the cameras in your game, list that you can do with GameObject.FindObjectsWithTag("Main Camera"); (note that there is a S in Objects) or you can use Unity's feature Camera.allCameras (http://docs.unity3d.com/ScriptReference/Camera-allCameras.html) but it takes only the enabled cameras of the scene so you'll need to disable everything afterwards ... and if you have other cameras that you don't wanna use, you can't. Anyway, i'd use the FindObjectsWithTag method :)

So we need a couple variables, to store all the cameras, to store the current camera and an index to circle through the cameras. Let's say you want to have 5 different cameras.

 var allCams : GameObject[5]; //That's an array, note the square brackets in the type.
 var curCam : GameObject; //Camera in use
 var index : int; //Index

If you want more (or less) cameras, you need to change the 5 value in GameObject[5]. Now we need to initialize them:

 function Start () {
    index = 0;
    allCams = GameObject.FindObjectsWithTag("Main Camera"); //We take the cameras we need
    curCam = allCams[index]; //We say we use first camera in the array
 
    //We deactivate all cams to only set active the one we want
    for (var cam in allCams){
       cam.SetActive(false);
    }
    curCam.SetActive(true);
 }

Now we want to circle through the cameras if user presses V → we want to circle through the array and we'll use index for that:

 function Update () {
    if (Input.GetKeDown(KeyCode.V)){
       curCam.SetActive(false); //Curent cam is disabled
       index += 1; //We step forxward in the array
 
       if(index > allCams.Length) {index = 0;} //If we are out of range, go back to 0
 
       curCam = allCams [index]; //We pick the new camera in the array
       curCam.SetActive(true); //We activate it
    }
 }

Now that you have everything you need to build your script. BUT, there are a few things you need to now:

  • I'm not very familiar with JS so it might not directly work

  • When you use FindObjectsWithTag you can't control the order in the array so you might want to build your array differently, see Array.Push() or a public array that you'd fill in the inspector ...

  • curCam is not very useful, it's only here to store allCams[index], you can get rid of it (and use allCams[index] instead), i just find it makes the script easier to read.

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 evil_debellator · Aug 11, 2015 at 11:27 AM 0
Share

Thanks mate, works like a charm but I ended up tweaking it a lot to fit my own tastes. I have a few more scripts which reference the cameras like my older system, time to fix them all.

^_^

X_X

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

2 People are following this question.

avatar image avatar image

Related Questions

connecting unity camera with realworld camera 0 Answers

Can I limit touch input for camera movement to non-ui areas? 1 Answer

How do I make Pixel Perfect Camera (Script) affect on specific sotring layer? 0 Answers

How do you give each player a separate camera? 1 Answer

How could i make a collision happen based on camera view ? 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