- Home /
Only activate scripts when game is toggled to the first person controller
Hello, My game toggles between a first and third person controller. I have a script on a cube which I only want to work when the game is in first person, and not to work in third person.
This is the script:
function OnMouseOver ()
{ rigidbody.velocity = transform.up * 05;
yield WaitForSeconds (5);
rigidbody.velocity = transform.up * 0;
}
After looking around on the forums, I still can't work out how to change it so it is only activated in first person - I was wondering if anyone knew how to change it, or give me a pointer for what to look for?
Thanks so much, Laurien
How are you making the change from First to Third person views?
This is the script for toggling between views:
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() {
player01.transform.position = player02.transform.position;
if (Input.Get$$anonymous$$eyDown ("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 = false;
}
check = !check;
}
}
I tried to activate and deactivate the script on the cube this way, but it's not working.
var firstPerson : GameObject;
var thirdPerson : GameObject;
function On$$anonymous$$ouseOver () {
if (firstPerson.active == true); {
rigidbody.velocity = transform.up * 05;
yield WaitForSeconds (5);
rigidbody.velocity = transform.up * 0;
}
else { (firstPerson.active == false);
rigidbody.velocity = transform.up * 0;
}
}
Thanks!
Answer by Fornoreason1000 · Apr 29, 2013 at 10:24 PM
ok your deactivating Character Controllers but you aren't disabling the players themselves, so when you check firstperson.active it will always be true.
so for your cube script. use
if(firstperson.GetComponent(CharacterController).active == true)
instead of
firstperson.active
Oh no - that's still giving me errors! Also - does that not apply to both character controllers, as there will always be either the first person or third person controller active, but I only want it to apply to the first person controller?
Changing it to the new line throws me this:
Assets/Game Scripts/Important Scripts/testOn$$anonymous$$ouseOverVelocity1.js(12,21): BCE0044: expecting :, found '='.
thats weird there nothing like that, Oh wait that a syntax error, you have Syntax errors. and a spelling mistake. dont worry fixed them
var firstPerson : GameObject;
var thirdPerson : GameObject;
function On$$anonymous$$ouseOver () {
if (firstPerson.GetComponent(CharacterController).active) {
rigidbody.velocity = transform.up * 10;
yield WaitForSeconds (5);
rigidbody.velocity = transform.up * 0;
}
else if (!firstPerson.GetComponent(CharacterController).active) {
rigidbody.velocity = transform.up * 0;
}
}
Wow thanks so much!!! That's really helped a lot :) (thanks)