- Home /
Toggle between first and third person - how do I toggle back?
Hello,
I want to toggle between first and third person, with the third person character not being visible whilst the game is in first person and vice versa.
This is the script I have so far (JavaScript), which switches the game into third person on pressing the return key - but I can't work out what code to use to switch the controller back again.
(I assume I have to use boolean? - but I've tried lots of different ways of writing it and I just can't work it out)
var cam01 : GameObject; // first person camera
var cam02 : GameObject; // third person camera
var player01 : GameObject; //first person controller
var player02 : GameObject; //third person controller
//start with first person active
function Start() {
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player02.GetComponent(CharacterController).active = false;
}
function Update() {
if (Input.GetKeyDown ("return")) {
cam01.gameObject.active = false;
cam02.gameObject.active = true;
player01.GetComponent(CharacterController).active = false;
player02.GetComponent(CharacterController).active = true;
}
}
Any help would be so much appreciated!
Thanks, Laurien
Answer by zzeettoo · Apr 29, 2013 at 04:20 PM
Just use a simple boolean variable to control on which state you are. this way, you can use the same procedure to switch in both ways...
var cam01 : GameObject; // first person camera
var cam02 : GameObject; // third person camera
var player01 : GameObject; //first person controller
var player02 : GameObject; //third person controller
var check; // New check-variable
//start with first person active
function Start() {
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player02.GetComponent(CharacterController).active = false;
check = true;
}
function Update() {
if (Input.GetKeyDown ("return")) {
if(check) {
cam01.gameObject.active = false;
cam02.gameObject.active = true;
player01.GetComponent(CharacterController).active = false;
player02.GetComponent(CharacterController).active = true;
}
else {
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.GetComponent(CharacterController).active = true;
player02.GetComponent(CharacterController).active = falsee;
}
check = !check;
}
}
Wow!! Thanks so much!!! I understand what I was doing wrong now :)