SwitchCamera

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Simplified by using more specific types)
Line 1: Line 1:
 
Author: JakeH
 
Author: JakeH
 
==Description==
 
==Description==
This script lets you switch between cameras in your scene.
+
This script lets you switch between two cameras in your scene.
  
 
==Usage==
 
==Usage==
Attach the script any GameObject and define the variables.
+
Attach the script to amy GameObject and define the variables.
  
 
==JavaScript- SwitchCamera.js==
 
==JavaScript- SwitchCamera.js==
 
<javascript>
 
<javascript>
var camera1 : GameObject;  
+
var camera1 : Camera;  
var camera2 : GameObject;  
+
var camera2 : Camera;  
  
 
function Start () {  
 
function Start () {  
   camera1.camera.enabled = true;  
+
   camera1.enabled = true;  
   camera2.camera.enabled = false;  
+
   camera2.enabled = false;  
 
}  
 
}  
  
 
function Update () {  
 
function Update () {  
 
   if (Input.GetKeyDown ("2")){  
 
   if (Input.GetKeyDown ("2")){  
       camera1.camera.enabled = false;  
+
       camera1.enabled = false;  
       camera2.camera.enabled = true;  
+
       camera2.enabled = true;  
 
   }  
 
   }  
 
   if (Input.GetKeyDown ("1")){  
 
   if (Input.GetKeyDown ("1")){  
       camera1.camera.enabled = true;  
+
       camera1.enabled = true;  
       camera2.camera.enabled = false;  
+
       camera2.enabled = false;  
 
   }     
 
   }     
 
}
 
}
Line 35: Line 35:
 
public class SwitchCamera : MonoBehaviour {
 
public class SwitchCamera : MonoBehaviour {
 
 
public GameObject camera1;
+
public Camera camera1;
public GameObject camera2;
+
public Camera camera2;
  
 
// Use this for initialization
 
// Use this for initialization
 
void Start () {
 
void Start () {
camera1.camera.enabled = true;  
+
camera1.enabled = true;  
camera2.camera.enabled = false;  
+
camera2.enabled = false;  
 
}
 
}
 
 
Line 48: Line 48:
 
if (Input.GetKeyDown("2"))
 
if (Input.GetKeyDown("2"))
 
{
 
{
camera1.camera.enabled = false;
+
camera1.enabled = false;
camera2.camera.enabled = true;
+
camera2.enabled = true;
 
}
 
}
 
if (Input.GetKeyDown("1"))
 
if (Input.GetKeyDown("1"))
 
{
 
{
camera1.camera.enabled = true;
+
camera1.enabled = true;
camera2.camera.enabled = false;
+
camera2.enabled = false;
 
}
 
}
 
}
 
}
 
}
 
}
 
</csharp>
 
</csharp>

Revision as of 10:21, 28 July 2010

Author: JakeH

Contents

Description

This script lets you switch between two cameras in your scene.

Usage

Attach the script to amy GameObject and define the variables.

JavaScript- SwitchCamera.js

<javascript> var camera1 : Camera; var camera2 : Camera;

function Start () {

  camera1.enabled = true; 
  camera2.enabled = false; 

}

function Update () {

  if (Input.GetKeyDown ("2")){ 
     camera1.enabled = false; 
     camera2.enabled = true; 
  } 
  if (Input.GetKeyDown ("1")){ 
     camera1.enabled = true; 
     camera2.enabled = false; 
  }     

} </javascript>

CSharp- SwitchCamera.cs

<csharp> using UnityEngine; using System.Collections;

public class SwitchCamera : MonoBehaviour {

public Camera camera1; public Camera camera2;

// Use this for initialization void Start () { camera1.enabled = true; camera2.enabled = false; }

// Update is called once per frame void Update () { if (Input.GetKeyDown("2")) { camera1.enabled = false; camera2.enabled = true; } if (Input.GetKeyDown("1")) { camera1.enabled = true; camera2.enabled = false; } } } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox