- Home /
Script to disable MouseLook?
I'm trying to disable MouseLook on my character controller using the following script, but I can't seem to get it to work. Any ideas?
#pragma strict
var IsMouselookOff : boolean = false ;
function Update()
{
if(Input.GetKeyDown("F12")) {
if (GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled == true){
disableCamera();
}
}
else if(Input.GetKeyDown("F12")){
if (GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled == false){
enableCamera();
}
}
}
function disableCamera()
{
GameObject.Find("Player").GetComponent(MouseLook).enabled = false;
GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled = false;
}
function enableCamera()
{
GameObject.Find("Player").GetComponent(MouseLook).enabled = true;
GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled = true;
}
Answer by ArkaneX · Oct 14, 2013 at 07:58 PM
Assuming your First Person Controller is named Player and related camera is named PlayerCamera, then there are two bugs in your script:
1) Function key names must contain lowercase f letter, so in your case you have to use f12 instead of F12. Instead of string names, I suggest using KeyCode enumeration.
2) Having the same condition for if
and then for else if
will cause that code inside else if
will never be called. I suggest following solution:
#pragma strict
private var _playerMouseLook : MouseLook;
private var _playerCameraMouseLook : MouseLook;
function Awake() {
_playerMouseLook = GameObject.Find("Player").GetComponent(MouseLook);
_playerCameraMouseLook = GameObject.Find("PlayerCamera").GetComponent(MouseLook);
}
function Update() {
if(Input.GetKeyDown(KeyCode.F12)) {
enableCamera(!_playerMouseLook.enabled);
}
}
function enableCamera(enable : boolean) {
_playerMouseLook.enabled = enable;
_playerCameraMouseLook.enabled = enable;
}
Answer by DylanW · Oct 14, 2013 at 07:58 PM
I believe your problem is your if and else statement within function Update.
Try this instead:
function Update()
{
if(Input.GetKeyDown("F12")) {
if(GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled == true) {
disableCamera();
}
else if(GameObject.Find("PlayerCamera").GetComponent(MouseLook).enabled == false) {
enableCamera();
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Cursor Lock to Center of screen script doesnt work please help. 2 Answers
Disable script from code 5 Answers
GuiTexture Width Change 1 Answer
Help needed with a pickup script 2 Answers