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 unityman749 · Nov 22, 2013 at 12:23 AM · camerainputcharacterswitch

How to switch between four characters when a button is pressed?

I am making a game with four characters with one camera attached to each of them. when I press the z button, I want to switch to the next camera and enable "player controller", a component in each of the characters, but only enable it for the character the active camera is attached to

Comment
Add comment · Show 1
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 Huacanacha · Nov 22, 2013 at 01:13 AM 0
Share

You should be able to do something like:

 Camera.main.enabled = false;
 camera.enabled = true;

On a script attached to the game object with the camera you want to enable.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ThatsAMorais · Nov 22, 2013 at 02:21 AM

Since you have 4 of them, you want to setup a round-robin that toggles the "enabled" state of each camera accordingly. So, in a script, in the Update() function, check for the 'z' key like this. I just tested this script by attaching it to an empty object I added to my scene, and I added 4 cameras to the scene. Be sure to import Collections.Generic for List or just use arrays. You also might want to toggle/disable the audio-listeners as well since Unity will complain about there being multiple.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CamSwitcher : MonoBehaviour {
 
     // Private monobehavior members
     int currentCameraIndex;
     List<Camera> characterCameras;
     
     void Start()
     {
         characterCameras = new List<Camera>();
         /// Get the cameras from the scene using Find or as a Parameter
         Camera camera1 = GameObject.Find("Camera1").camera;
         characterCameras.Add(camera1);
         Camera camera2 = GameObject.Find("Camera2").camera;
         characterCameras.Add(camera2);
         Camera camera3 = GameObject.Find("Camera3").camera;
         characterCameras.Add(camera3);
         Camera camera4 = GameObject.Find("Camera4").camera;
         characterCameras.Add(camera4);
     }
     
     
     void Update()
     {
         if(Input.GetKeyUp(KeyCode.Z))
         {
             // Increment the camera index to the next camera in the list
             currentCameraIndex++;
             if(characterCameras.Count == currentCameraIndex)
             {
                 currentCameraIndex = 0;
             }
             
             // loop over the camera list, disabling all but the chosen index
             foreach(Camera camera in characterCameras)
             {
                 if(camera == characterCameras[currentCameraIndex])
                 {
                     camera.enabled = true;
                 }
                 else
                 {
                     camera.enabled = false;
                 }
             }
         }
     }
  }
Comment
Add comment · Show 2 · 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 unityman749 · Nov 22, 2013 at 03:18 AM 0
Share

thanks! One question, do I have to change any variables in your script and what do I add the script to?

avatar image ThatsAMorais · Nov 22, 2013 at 03:48 AM 0
Share

I'll edit my answer with a little more description. You could attach it to almost anything that will be alive in the scene, but what I do is make an Empty gameobject and add the script to that, from the editor.

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

18 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

Related Questions

character switching on collision 1 Answer

switch character 1 Answer

How can I change the character beign controled and the camera with a button? 1 Answer

changing player (getting in vehicles) 3 Answers

setting a character controller orientation by script 1 Answer


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