- Home /
Switching between controllers - why do I have to press spacebar twice before it works?
Hello - in my game the player can switch between first and third person on pressing the space bar. Once the third person controller hits a collider, after 3 seconds it switches to the first person controller automatically without the player pressing space.
The script to SwitchBetweenControllers works, and the one to ForcePlayerIntoFirstPerson works - but my problem is that once the player is in first person - he has to press the spacebar twice before switching to the third person controller.
I can't work out why this is - any help would be so much appreciated!
This is the script for SwitchBetweenControllers (JavaScript):
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; // check-variable
//start with first person active
function Start() {
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player02.active = false;
check = true;
}
function Update() {
player01.transform.position = player02.transform.position;
if (Input.GetKeyDown ("space")) {
if(check) {
cam01.gameObject.active = false;
cam02.gameObject.active = true;
player01.active = false;
player02.active = true;
}
else {
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
}
check = !check;
}
}
And this is the script for ForcePlayerIntoFirstPerson (which is attached to the collider):
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;
function OnTriggerEnter(other: Collider){
if (other.tag == "Player")
{
yield WaitForSeconds (3);
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
check = true;
}
}
Thanks so much, Laurien
Answer by SubatomicHero · Jun 10, 2013 at 01:16 PM
OK so if they are two separate scripts, there are a couple of issues with your boolean checks.
In your ForcePlayerIntoFirstPerson script, you don't need a local variable called "check". You need to get access to the check variable in your SwitchBetweenControllers script. So your OnTriggerEnter() should be updated to this:
function OnTriggerEnter(other: Collider) { if(other.tag == "Player"){ yield WaitForSeconds(3);
cam01.gameObject.active = true; cam02.gameObject.active = false; player01.active = true; player02.active = false; var temp : SwitchBetweenControllers = gameObject.GetComponent(SwitchBetweenControllers); temp.check = true; } }
Then back in your ForcePlayerIntoFirstPerson script in your update function this maybe a bit neater:
function Update() {
if (Input.GetKeyUp("space")) {
if (check) check = false;
else check = true;
}
if(check) {
cam01.gameObject.active = false;
cam02.gameObject.active = true;
player01.active = false;
player02.active = true;
} else {
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
}
}
See if this helps!
The OP had the line check=!check;
which is rewritten here as the long way: if(check) check=false; else check=true;
. Now, maybe the OP didn't know what !check
meant, so this is better.
In other words, the only change in Update is moving "flip check" up, to before it gets used.
No I did, I was just trying to simplify it as much as possible :P
I've changed the line check = !check -- but I'm still having to press twice to go back to the third person controller, I can't work out why.
(I tried the new codes - but the second one came up with a lot of errors, and the switch between controllers stopped working properly)
Sorry. I just made it worse. The point is that the "new code" is really just 3 little changes to the original code.
One of the changes is just to move up check=!check
. subAtH also rewrote that with an if, so you might be able to understand it better.
In the ForcePlayer into first person - I just added the extra line of script - it does seem like it should work, but I'm still having to press the spacebar twice.