- Home /
Aim Down Sight Script not working with GUI Button, but working with MouseButtonDown(0)
I am making a fps game for windows phone. I have an Aim Down Sight script for my gun. It works fine with Input.GetMouseButton, but when I try and use the GUI function instead, it draws it but does nothing. Here is the original script: #pragma strict
var Aim : boolean = false; var Cam : GameObject;
function Update () { if(Input.GetMouseButtonDown(0)){ Aim = true; if(Aim == true){ Cam.active = true; } }
if(Input.GetMouseButtonUp(0)){ Aim = true; if(Aim){ Cam.active = false; }
}
}
Here is the script with GUI Functions:
enter code here#pragma strict
var Aim : boolean = false; var Cam : GameObject;
function OnGUI () { if(GUI.Button (Rect (20,40,80,20), "ADS")){
Aim = true; if(Aim == true){ Cam.active = true; } }
if(GUI.Button (Rect (20,40,80,20), "ADS")){
Aim = true; if(Aim){ Cam.active = false; }
}
}
I am much less confident with GUI input. I keep pressing it but it doesn't ADS like the MouseButton(0). I'd much rather have this as a GUI functionality, as it plays weird on my phone as a tap.
Thanks. BenDy Games, Game Development For Windows Phone 8
Please may someone help? I was thinking of $$anonymous$$ouseButtonUp for GUI, but there is none. I really need this, I will definetly upvote the best answer!
Answer by robertbu · May 03, 2014 at 02:14 PM
By convention, variables should start with a lower case letter. Functions and classes start with an upper case letter. Here is a bit of code that does with GUI what your original code was doing with OnMouseDown()/OnMouseUp():
#pragma strict
var aim : boolean = false;
var cam : GameObject;
private var rect = Rect(20,40,80,20);
function OnGUI () {
var e = Event.current;
if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition)) {
aim = true;
cam.SetActive(true);
Debug.Log("Finger Down");
}
if (e.type == EventType.MouseUp && rect.Contains(e.mousePosition)) {
aim = true;
cam.SetActive(false);
Debug.Log("Finger Up");
}
GUI.Button (Rect (20,40,80,20), "ADS");
}
Note I was a bit confused about 'aim' being set to 'true' for both the mouse down and the mouse up case. Are you sure that is what you want to do? Is there more to this script?
Thanks so much, it worked perfectly ;) The script is pretty small, but yeah, that's what I wanted to do ;)
Just wondering is there a way to do the same stuff, but is there a way to use the same script, but do it so i do not have to hold the button, just press it and it stays like that, and when I press it again it goes out of the ads. Sorry for bad grammar, I'm on my phone.
Try this:
#pragma strict
var aim : boolean = false;
var cam : GameObject;
private var rect = Rect(20,40,80,20);
function OnGUI () {
var e = Event.current;
if (e.type == EventType.$$anonymous$$ouseDown && rect.Contains(e.mousePosition)) {
aim = true;
cam.SetActive(!cam.activeSelf);
Debug.Log("Finger Down");
}
GUI.Button (Rect (20,40,80,20), "ADS");
}
Thanks so much dude, I will be featuring you in the credits of my game ;)