- Home /
Add multiple cameras problems
private var walkSpeed : float = 1.0; private var gravity = 100.0; private var moveDirection : Vector3 = Vector3.zero; private var charController : CharacterController; var Camera1 = GameObject.Find("Camera1");
var Camera2 = GameObject.Find("Camera2"); function Start() { charController = GetComponent(CharacterController); Camera2.enabled = true; Camera1.enabled = false; }
function Update () { if(charController.isGrounded == true) { if(Input.GetAxis("Vertical") > .1) {
walkSpeed = 2;
}
else if(Input.GetAxis("Vertical") < -.1)
{
walkSpeed = 1;
}
// Create an animation cycle for when the character is turning on the spot
transform.eulerAngles.y += Input.GetAxis("Horizontal");
// Calculate the movement direction (forward motion)
moveDirection = Vector3(0,0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
}
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
if (Input.GetKeyDown ("2")){
Camera1.enabled = false;
Camera2.enabled = true;
}
if (Input.GetKeyDown ("1"))
{
Camera1.enabled = true;
Camera2.enabled = false;
}
}
please give me a suggestion?.I want to switch between two cameras
Answer by duck · Oct 28, 2010 at 12:19 PM
You're calling "enabled" on the camera gameobjects themselves rather than the camera component on those gameobjects.
I'd advise storing a reference to those components directly by changing your initial 'var' lines from this:
var Camera1 = GameObject.Find("Camera1");
var Camera2 = GameObject.Find("Camera2");
to this:
var Camera1 = GameObject.Find("Camera1").camera;
var Camera2 = GameObject.Find("Camera2").camera;
Your answer
Follow this Question
Related Questions
Switching Several Cameras(HorrorGame) 1 Answer
Switching cameras with the mouse button 1 Answer
Multiple Camera.main.ScreenPointToRay 1 Answer
Character movement when I switch cameras. 0 Answers
nodal camera positions 1 Answer