Hello all
First post here. Just getting into unity and loving it. Find this forum very useful.
I’m using the first person controls and I have created a gun, firing using
if(Input.GetButtonDown(“Fire2”)){
firegun();
}
This fires when two touches are on the screen, “Fire1” with one touch, neither are much help as it fires when you are trying to moving around. So I created a gui texture fire button above the FPC this works great but I can’t move around using the FPC and use the gui texture at the same time.
What is the best way to implement a fire button using FPC?
In what way does the GUI texture interfere with the FPS controls? A gesture is probably too slow for a firing control, so I would think you are limited to having a fire button or else making the gun fire at objects as they are touched, etc.
It’s more the other way around the FPC always works fine but if you touch the screen anywhere and also the GUI texture then the gun will not fire(GUI texture doesn’t register a hit). I think I read somewhere if you touch twice it registers the hit in the middle of the two.
Unity can handle more than one touch at a time, but you will need to do a bit more coding. The iPhoneInput.touchCount property will tell you how many touches are on the screen at any one time and the iPhoneInput.GetTouch function will return a touch by its index number. Instead of using the GUITexture, you will probably need to get the position of the touch and check if it falls within the button’s rectangle. For example, you could draw a button with GUI.DrawTexture and then check for a click with code like this:-
var buttonRect: Rect;
var buttonTexture: Texture2D;
function OnGUI() {
GUI.DrawTexture(buttonRect, buttonTexture);
}
function Update() {
...
var touch = iPhoneInput.GetTouch(0); // Or whichever touch is appropriate.
if (buttonRect.Contains(touch.position)) {
// Fire gun.
}
...
}
(The exact code will depend on what you are doing, of course, but this should at least give you a start.)