- Home /
The question is answered, right answer was accepted
Swaping to a camera attached to another object
Looking at other scripts this one should work but it will not change the "state" variable.
I have manually changed this variable in game and the script switches cameras as it should, it just wont do it when I am pushing "e_key"
It shouldn't be a problem as I have doors using the same detection system and they work!
Would be great if anyone could give me a few pointers!
here is the code:
var player_cam : Camera; var cam : Camera;
var state : int = 0;
function Update ()
{
var hit : RaycastHit;
//find forward then test for x meters in front of position if (Physics.Raycast (transform.position, transform.forward, hit, 2))
{ if(hit.collider.gameObject.tag=="Player" && Input.GetAxis ("e_key") ) { if (state == 0) { state = 1; } if (state == 1) { state = 0; }
} }
if (state == 1) { player_cam.GetComponent("Camera").active = false; cam.GetComponent("Camera").active = true; } if (state == 0) { player_cam.GetComponent("Camera").active = true; cam.GetComponent("Camera").active = false; }
}
Answer by DaveA · Feb 01, 2011 at 11:37 PM
if (state == 0)
{
state = 1;
}
if (state == 1)
{
state = 0;
}
You set state = 1, then test if state == 1. It always will. You need an 'else' there instead, or just
state = !state; or state = 1-state;
Answer by tool55 · Feb 02, 2011 at 06:17 AM
I would use a boolean instead.
var camSwitch : boolean = true;
function Update () { var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, 2))
{ if(hit.collider.gameObject.tag=="Player" && Input.GetAxis ("e_key") ) { camSwitch = !camSwitch;
player_cam.GetComponent("Camera").active = camSwitch; cam.GetComponent("Camera").active = !camSwitch; } }
}
Also, since you're going to be accessing the other camera often (I presume) you might as well store it as a variable.
var player_cam : Camera;
//Then you can just write
player_cam.active = camSwitch.
You can just drag the camera into the player_cam variable in the inspector.
The other camera is locked to an array of turrets on a space ship. There will be an average of four turret arrays per ship + Driver/Commander "seat" - I have used an updated version of my script but if you can see that it will become inadequate, please post something - thanks for your post by the way, it has helped me with another script!
Well, if you have multiple turrents and cameras, you can use an array.
myCams : Camera[];
Then cycle through them. myCams [0], myCams [1], myCams [2] etc. You can put as many cameras in the array as you want in the inspector.
Follow this Question
Related Questions
How do I make an object appear differently to different players? 2 Answers
How can I turn off gameobjects icons in camera view? 0 Answers
moving objects relative to the player... 1 Answer
Camera looking through objects when touching 1 Answer
How do you dynamicly change a cameras culling mask based on an object selection? 1 Answer