Continuous Fire On Button Click

Hello ,

Here i want to fire continuously when user hold button…

For that i have created Plane game object with button texture… And Set collider on that plane game object…

When user hold that button i want to fire continuously … For that i have tried code which is below :

if(Input.GetMouseButton(0))
     {
     	 var hit  : RaycastHit;
         var ray  : Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray,hit))
         {
         	  if(hit.collider.gameObject.GetComponent("ButtonId"))
              {
              	  buttonPressed = hit.collider.gameObject.GetComponent("ButtonId");
              	  if(buttonPressed.id == "Fire")
                  {
                	 HandleBullet();			
                  }
              }
         }
     }

function HandleBullet()
{
	spawnPoint = GameObject.Find("Bullet");            
    if(spawnPoint != null)
	{
		if(playerScript.State != state.Explosion)
		{	
			bulletObject = Instantiate(projectilePrefab, spawnPoint.transform.position,spawnPoint.transform.rotation);
		}
	}
}

I have written this script… but its not working for rapid fire…

Anyone is having idea then please suggest me…

Thanx for ur support and help in advance…

what are the problems you are facing and what type of game is this so that i can help you with the fire code script

–

kind of space shooter 2d game.. ship moves right and left and shoot bullet..

–

you can make the GetMouseButtonDown(0) switch a boolean to true, which makes the gun fire continuosly and then you check GetMouseButtonUp(0) you witch it to false again.

–

Please tell me the mouse input is inside a function. Try Debugging at line 10 before the if : Debug.Log( "buttonPressed.id = " + buttonPressed.id );

–

fire works for single click.. if i want to fire again,then i need to click

–

1 Answer

1

Use get mouse button in update function and you are all set to go…Cheers from INDIA…

Don’t forget to mark the answer if found useful

as well as a free advice you can check space shooter 2d's tutorials of 3dbuzz.com for the same..... in that explaining each and every thing very clearly might be useful to you ........ [3dbuzz 2d space shooter link][1] [Constant fire][2] Enjoy....... [1]: http://www.3dbuzz.com/training/view/unity-standard/simple-2d-shooter [2]: http://www.3dbuzz.com/training/view/unity-standard/simple-2d-shooter/09-firing-the-projectile

–

I knw this is your fav tutorial.. Thanks.. Have already gone through it..

–

for your kind information follow that Constant fire link skip to 9:00 min and watch for a min that guy is explaining the whole thing there ........ i am not the author or sales person for 3dbuzz but i found it useful so mentioned here and one more thing sorry to say but if you ever gone threw that tutorial then there is not a single chance for this kind of question to arise .........

–

k.. thnx...

–