- Home /
How would I use a Gui Button to shoot?
I need help in putting a shoot function for android. I have Gui Button now how would I could so basically my gun would shoot
I just want to replace the "fire1" command which is used to shoot in PC/Mac Fps with pressing a Gui Button.
Could someone please help me out?
Thanks
Answer by save · Jul 18, 2013 at 09:14 AM
What probably would be best in your case is to break the shooting out to a function. then just call that from either when Input.GetAxis("Shooting") returns true or when your Gui Button is interacted with. A RepeatButton would probably be suitable if you want to check that the button is pressed over several frames. What you need to do as well is to create a separate axis for shooting because Fire1 is automatically called when interacting on iOS and Android. Another way of solving it would be to check Input.GetMouseButton(0) (which in most cases is left mouse button) - but, which would limit the user to not be able to change it in the input configuration.
function OnGUI () {
if (GUI.RepeatButton(Rect(10,70,50,30),"Shoot"))
GoTriggerHappy();
}
function Update () {
if (Input.GetAxis("Shoot"))
GoTriggerHappy();
}
function GoTriggerHappy () {
// All events for shooting goes here
}
So basically I take my gun script and put it on the go trigger happy place? And then put this script on my gui button? But I encountered a problem before the button fire 1 is activated if one finger is touches the screen of the phone how would I disable that and only make it if it touches the button?
Ah yes forgive me, Fire1 is always called upon interaction on iOS and Android. What you need to do is to have a separate name for the input axis to fire or call Input.Get$$anonymous$$ouseButton(0) ins$$anonymous$$d.
I always use Input.Get$$anonymous$$ouseButton(0) because this is in most causes just for testing purposes in the editor where user editing of input buttons isn't a necessary function, then check for interaction with Input.touchCount>0.
I'm not sure what your structure would be with script placements. Try to gather all interaction events under one script where you also have all your references to all GameObjects which in some way is a part of the interaction. It's a classic method to not go crazy after a while. :) Then of course some events must happen outside of that script, but that's usually just a call from your interaction script to a function inside the other script.
And so basically replace fire button with get mouse button 0 ? And do what you said?
Thank you so much!!! You saved my life :D I will pay you back if I get somewhat famous ;)
Haha no need to, you just have to make great games and I'll play them. :) I updated my previous comment.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Mobile FPS Help 0 Answers
Why my game's FPS drop every time I change scene (Android Game) ? 1 Answer
Calling static jar function from Unity3D 0 Answers
constantForce and relativeForce errors. 0 Answers