• 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
Question by Dev · Apr 02, 2010 at 11:16 AM · cameraswitchrelative

Switching between cameras

Hi, I would like to switch between cameras depending on the position of my player position, how can I do this please?

Thanks a lot for your time.

Comment
Chris Masterton
Jean-Fabre
Myth
edthethird
BiG
Zishii
Darthmaydar
Salim

People who like this

8 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

4 Replies

  • Sort: 
avatar image
Best Answer

Answer by Jason_DB · Apr 02, 2010 at 01:55 PM

You can activate and deactivate cameras if you can get a hold of the object that holds them.

//cameraObject is the gameObject which holds the camera

cameraObject.camera.active = false;

So If you had a script which kept track of all the cameras in your scene then you could switch cameras easily. For example here is a function which would go through an array of camera objects and turn a certain one on (based on the index sent to it).

var cameras : GameObject[];

function SelectCamera (index : int) { for (var i : int=0 ;i<wcameras.length; i++) { // Activate the selected camera if (i == index){ cameras[i].camera.active = true; // Deactivate all other cameras }else{ cameras[i].camera.active = false; }

}

You could use this function by calling it with a message

BroadcastMessage ("SelectCamera", 1);

or by outright calling it

SelectCamera(1);

You just need to get hold of the instance of the script it's in

Comment
Philbywhizz
askerer
Jean-Fabre
mikevanman
Myth
user-1618 (google)
user-11073 (google)
edthethird
BiG
yotingo
jdelaay025
Salim

People who like this

13 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
avatar image

Answer by straydogstrut · Apr 02, 2010 at 02:02 PM

DastardlyBanana's answer would be more flexible, but i'll post my (very simple!) answer here anyway:

You could check for a collision with a collider and then enabled/disable your cameras, so:

var Camera1 : Camera; var Camera2 : Camera;

function Start(){ Camera1.enabled = true; Camera2.enabled = false;

}

function OnTriggerEnter (other : Collider) {

 if(other.gameObject.name == "switch_camera"){

     Camera1.enabled = false;
     Camera2.enabled = true;

 }

}

function OnTriggerExit (other : Collider) {

 if(other.gameObject.name == "switch_camera"){

     Camera1.enabled = true;
     Camera2.enabled = false;

 }

}

Camera1 and Camera2 are assigned in the editor, or you could assign them with code.

Cheers.

Comment
askerer
AVividLight
Jean-Fabre
Myth
Richard 3
jdelaay025

People who like this

6 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
avatar image

Answer by askerer · Apr 22, 2010 at 09:19 AM

Thanks because I've just looked at your answer I had a lot o thngs to do but it's not as interresting as your answer thanks to you 2!!

Comment
BiG
vargata

People who like this

0 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 spinaljack · Apr 22, 2010 at 02:36 PM 0
Share

Keep comments off of the answers area

avatar image

Answer by ImRipken · Oct 07, 2016 at 10:03 PM

This script worked for me. It only works for 2 camera's as far as I know but you might be able to edit it.

var cam1 : Camera; var cam2 : Camera;

function Start() { cam1.enabled = true; cam2.enabled = false; }

function Update() {

 if (Input.GetKeyDown(KeyCode.C)) {
     cam1.enabled = !cam1.enabled;
     cam2.enabled = !cam2.enabled;
 }

}

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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

How to give camera relative movement to the player and I am using joystick for player and camera 2 Answers

How to switch between cameras/players? 1 Answer

defining a relative axis of rotation 1 Answer

cameras switch doesn't work 1 Answer

How to set main camera? 3 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