- Home /
Stop shooting infinite bullets with touch
Hey guys, I am trying to adapt my touch script. Touch is my weakest point I believe.
Here I have this code...
void Update ()
{
Touch touch;
if(Input.touchCount > 0 )
{
touch = Input.touches[0];
if(targetTex.HitTest(touch.position))
{
if (touch.phase == TouchPhase.Began)
targetTex.texture = touchTex;
if(moveCtrl.facingRight)
{
// ... instantiate the BulletPrefab facing right and set it's velocity to the right.
Rigidbody2D bulletObject = Instantiate(BulletPrefab, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletObject.velocity = new Vector2(speed, 0);
}
else
{
// Otherwise instantiate the BulletPrefab facing left and set it's velocity to the left.
Rigidbody2D bulletObject = Instantiate(BulletPrefab, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
bulletObject.velocity = new Vector2(-speed, 0);
}
}
}
This shoots so many bullets as long as my finger is down. I want it that when I tap it, the bullet fires once and onces only... How can I do this? Im sure its the way I have set the touch stuff but I can't seem to figure it out.
Answer by Simon-Larsen · Mar 30, 2014 at 10:32 PM
I wrote a little example code for you to look at. Whether you will use it or not is up to you. You will need to integrate it with your own script ,but basically this script fires a bullet at a specified fire rate. Best of luck on your project
public class ShootBullets : MonoBehaviour
{
// Bullets per second
public float fireRate = 5f;
float fireTimer;
void Start () {
fireTimer = Time.time;
}
void Update ()
{
// Check for touch input as well as mouse input -
// so we can test in the editor
if (Input.touchCount > 0 || Input.GetMouseButton(0)) {
if (Time.time >= 1/fireRate + fireTimer) {
// Set the time at which the last bullet was fired
fireTimer = Time.time;
// Do whatever you do to spawn your bullets here
Debug.Log ("Firing a bullet!");
}
}
}
}
Answer by trololo · Mar 30, 2014 at 10:00 PM
Set a boolean to true at TouchPhase.Began and to false at TouchPhase.Ended, and and use it to fire only once (when it's false).
You could also just fire once when you get a TouchPhase.Ended For more info on this https://docs.unity3d.com/Documentation/ScriptReference/Touch-phase.html
Hi thanks for the answer. I believe I may have not worded it right...
$$anonymous$$y player can shoot, and shoots when I touch the screen and stop when I let go. However, it shoots so many times when my finger is on the screen, and I would like this to only happen once.
A quick tap on the button shoots like 3 bullets.
(This was meant for Trololo. Didn't see above comment at time of commenting)
Your comment is misleading. Do you want to shoot only one time per tap? Or do you want to fire a couple bullet within a certain interval per tap?
It has been a long day sorry.
I would like it to shoot once on every tap.
Now thinking about it, I could also implement shooting a couple bullets at certain interval into my game too!
So yeh, either will work for me
Put everything after the touch.phase condition inside it.
Answer by br.glen23 · Mar 30, 2014 at 10:47 PM
I had a variation of this problem when implementing a toggle button. When the button was down it would switch between states very rapidly.
Basically, I just implemented an extra boolean variable so that an action would only happen once..
var toggle : boolean = true;
if (touch.phase == TouchPhase.Began){
if(toggle == true){
//fire bullet
}
//after it fires the first bullet it sets toggle to false to it won't fire again
toggle = false;
}
if (touch.phase == TouchPhase.Ended){
toggle = true;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613141550im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I go about using Hit.Test? 1 Answer
How to render a colored 2D rectangle. 6 Answers
Windows 7 touch not working correctly in WebGL! 1 Answer
Help with 2d shooting 1 Answer
2D GUI accessibility 2 Answers