x


Same commands firing multiple times android

hey whenever i touch my screen where i have defined it shoots but takes off a random number sometimes 1 sometimes 4 or 5 here is my code:

here is where i call the event:

if( ShootTouchPad.IsFingerDown())

{

Attack();

         BulletsLeft -= 1;

         if(BulletsLeft < 0)
         {
          BulletsLeft = 0;
         }

         if(BulletsLeft == 0)
         {
          Reload();
         }
    }

and here is the command called:

function Attack()

{

Debug.Log("Shot");

var Hit : RaycastHit;

var DirectionRay = transform.TransformDirection(Vector3.forward);

Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
if(Physics.Raycast(transform.position, DirectionRay, Hit, Range))
{
    if(Hit.rigidbody)
    {
       Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
    }
}
more ▼

asked Apr 25 '12 at 06:59 AM

thommoboy gravatar image

thommoboy
24 1 9 19

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I would do It Like This:

var BulletsLeft : int =5;

var Ready2Shoot : System.Boolean = true;

function Update() {

 if( ShootTouchPad.IsFingerDown()){
     if(BulletsLeft < 0){
         Attack();
     if(BulletsLeft == 0){  
          Ready2Shoot = false;  
          Reload();
     }

Function Reload(){

 BulletsLeft = 5;
 //Reload ANim stuff
 //When you are done...
 Ready2Shoot = true;
}

function Attack(){

 `//Attack stuff`

BulletsLeft = BulletsLeft - 1; }

more ▼

answered Apr 25 '12 at 12:50 PM

programmrzinc gravatar image

programmrzinc
514 4 10 15

would this also fix the minusing more than one bullet per shot?

Apr 25 '12 at 10:41 PM thommoboy

I think so

Apr 27 '12 at 12:47 PM programmrzinc
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3460
x2482

asked: Apr 25 '12 at 06:59 AM

Seen: 262 times

Last Updated: Apr 27 '12 at 12:47 PM