Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ilkzz · Mar 30, 2014 at 09:43 PM · 2dguitouchshooting

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!");
             }
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ilkzz · Mar 30, 2014 at 10:50 PM 0
Share

Thank you for this. It will definitely come in handy!

avatar image
1

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).

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Simon-Larsen · Mar 30, 2014 at 10:03 PM 0
Share

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

avatar image Ilkzz · Mar 30, 2014 at 10:06 PM 0
Share

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)

avatar image Simon-Larsen · Mar 30, 2014 at 10:10 PM 0
Share

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?

avatar image Ilkzz · Mar 30, 2014 at 10:14 PM 0
Share

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

avatar image trololo · Mar 30, 2014 at 10:22 PM 1
Share

Put everything after the touch.phase condition inside it.

Show more comments
avatar image
0

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges