Here is my code, I set it so that you can hit any of the buttons no matter if there is already another finger touching the screen or not, but it doesn’t seem to be working.
function Update () {
if (iPhoneInput.touchCount >= 0){
for (var evt : iPhoneTouch in iPhoneInput.touches){
var touch : iPhoneTouch = iPhoneInput.touches[1];
var touch2 : iPhoneTouch = iPhoneInput.touches[0];
var pauseButtonHit = pauseButton.HitTest(touch.position);
var fireButtonHit = fireButton.HitTest(touch.position);
var reloadButtonHit = reloadButton.HitTest(touch.position);
var reloadButtonHit2 = reloadButton.HitTest(touch2.position);
var pauseButtonHit2 = pauseButton.HitTest(touch2.position);
var fireButtonHit2 = fireButton.HitTest(touch2.position);
if (evt.phase == iPhoneTouchPhase.Ended){
if (pauseButtonHit) {
if (Time.timeScale == 0.0) Time.timeScale = 1.0;
else Time.timeScale = 0.0;
}
else if (pauseButtonHit2) {
if (Time.timeScale == 0.0) Time.timeScale = 1.0;
else Time.timeScale = 0.0;
}
if(bulletAmmo == true){
if (fireButtonHit)
{
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (speed,0,0));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
limitedAmmo --;
}
else if (fireButtonHit2)
{
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (speed,0,0));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
limitedAmmo --;
}
}
else if(reload == true){
if(reloadButtonHit2){
reloadsLeft --;
limitedAmmo = 10.0;
}
else if(reloadButtonHit){
reloadsLeft --;
limitedAmmo = 10.0;
}
}
if(limitedAmmo <= 0.0){
bulletAmmo = false;
}
else{
bulletAmmo = true;
}
if(reloadsLeft <= 0.0){
reload = false;
}
else{
reload = true;
}
}
Has any one helped you yet, as I have the same issue. I used the Penelope controllers for movement and added and added fire button. Well I can fire the projectile but only when I am not moving. I am super stuck.
I don’t know if I’m missing something subtle here, but it seems as though you are iterating through the touches with the loop, but then basing the fire button hit on the value of the touch variable. This is set to touches[0] at the start of the loop - I think you want to use the evt variable rather than touch for the button detection.
You may also want to look at some of the several sprite based GUI solutions like GUIManager, SpriteUI, or others. Forum search, unfortunately. I’ve used SpriteUI very successfully, but I’ve not tried to combine it with the Penelope control scripts.
Here’s some code I use to detect hits on GUI Textures:
private var myguiLayer : GUILayer;
function Start() {
var tempObject = GameObject.Find("ObjectWithTheGUILayer");
myguiLayer = tempObject.transform.GetComponent(GUILayer);
}
// -----------------------------------------
// -----------------------------------------
// Detect touch on object/world modifier
// -----------------------------------------
// -----------------------------------------
function detectHitOnModifier() {
var element : GUIElement;
var touchPosition : Vector2;
var guiTouch : iPhoneTouch;
if (iPhoneInput.touchCount > 0) {
for (var touch : iPhoneTouch in iPhoneInput.touches) {
touchPosition = touch.position;
element = myguiLayer.HitTest ( touchPosition );
}
}
if (iPhoneInput.touchCount > 0 ) {
touch1 = iPhoneInput.GetTouch(0);
element = myguiLayer.HitTest( touch1.position );
if (element) {
if (element.name=="objectNameImTryingToHit" ) {
// Do stuff...
} else if (element.name=="someOtherObject" ) {
// Do other stuff...
}
}
}
}
// -----------------------------------
// -----------------------------------
I don’t actually use it much at the moment, because I’ve moved to other methods of buttons, so it’s maybe not as clean as it could be (checking for ‘name’ is probably not the best way to go) but it worked well for me when I did use it. Hope this helps!
(btw, I think this was probably cribbed from somewhere else…)