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 JayOhhh · Apr 20, 2014 at 11:07 PM · 2dcoroutineshooteronmousedrag

Shooting bullets OnMouseDrag, time delay issues

Hello,

I'm fairly new with OnMouseDrag and Coroutines in general.

What I want to accomplish:

I currently have movement setup on mouse drag. You drag the character back and forth across the screen. I want the character to be shooting while you are dragging.

Issue I am having:

It seems that OnMouseDrag fires a ridiculous amount of times. I have a BulletFire script that is being called from OnMouseDrag, and it is fireing multiple times per second. This results in wayyyy to many bullets being shot.

What I tried:

I've tried calling a coroutine from my OnMouseDrag method, but that routine gets called so many times, its actually doing nothing but delaying the initial shot.

     IEnumerator wait(){
         yield return new WaitForSeconds (0.5f);
         bulletSpawnScript.FireBullet ();               
     }   
 
     void OnMouseDown()
     {
         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);            
         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
 
     }
     
     void OnMouseDrag()
     {
 
         Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         point.z = gameObject.transform.position.z;
         point.y = gameObject.transform.position.y;
         gameObject.transform.position = point;
         //Shoot gun
         StartCoroutine (wait ());
         //callFireBullet ();
     }
 
     void callFireBullet() {
         StartCoroutine (wait ());
     }

So basically here i'm trying to call my shoot method while the mouse button is held down, but I want to be able to have a .5 second or 1 second delay in between calls. Any idea what I am doing wrong?

Help is appreciated! Thanks, JayOhhGames

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 Pyrian · Apr 20, 2014 at 11:30 PM

So, you'll want to track whether a bullet has been fired in the past half-second. Just set a bool to false, set it true when you fire, and set it back to false in the coroutine. Test that bool to decide whether to fire.

 private bool RecentShot = false;
 
 IEnumerator wait()
 {
     yield return new WaitForSeconds (0.5f);
     RecentShot = false;
 }
 
 void OnMouseDrag()
 {
     // Do your stuff
     if (!RecentShot)
     {
         RecentShot = true;
         callFireBullet ();
         StartCoroutine (wait ());
     }
 }
 
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 JayOhhh · Apr 20, 2014 at 11:49 PM 0
Share

Thanks this is exactly what I was looking for! Such a simple solution as it always is with me :)

avatar image
1

Answer by Rydrako · Apr 20, 2014 at 11:35 PM

I believe what you want to do is just use a while loop! That way it calls it again when it's done! Also make a new variable and while to check for it and when MouseDown set it to true and on MouseUp make it false!

 boolean mouseIsDown;
 
 IEnumator wait()
 {
      while(mouseIsDown)
      {
           yield return new WaitForSeconds (0.5f);
           bulletSpawnScript.FireBullet ();
           callFireBullet();  
      }
 }
 
 void OnMouseDown ()
 {
      //your code
      mouseIsDown = true;
 }
 
 void OnMouseUp ()
 {
      mouseIsDown = false;
 }

It should result in the coroutine looping and executing only when the mouse is held down.

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 JayOhhh · Apr 20, 2014 at 11:50 PM 0
Share

I tried this method, and I still had the bullets firing in rapid succession. I ended up going with the first answer provided, that seemed to do what I was looking for! Thanks for the quick response though :)

avatar image
1

Answer by $$anonymous$$ · May 30, 2014 at 12:06 AM

ok, I fixed up my code for it to work, monodevelop compiles it. then Unity gives me an error. Assets/dimensionswitcher.cs(6,20): error CS1519: Unexpected symbol =' in class, struct, or interface member declaration this is my new code: using UnityEngine; using System.Collections; public class dimensionswitcher : MonoBehaviour { bool dimension1 = 0; dimension1 = true bool dimension2 = 1; dimension2 = true void Update () { call waitswitch StartCoroutine (waitswitch()); if dimension1 = true { if dimension2 = true { if (Input.GetKeyDown ("z")) transform.position = new Vector3 (transform.position.x, transform.position.y, dimension1); if (Input.GetKeyDown ("x")) transform.position = new Vector3 (transform.position.x, transform.position.y, dimension2);
} } } } IEnumerator waitswitch(){ dimension1 = false dimension2 = false yield return new WaitForSeconds (3.0f); dimension1 = true dimension2 = true }

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 JayOhhh · May 30, 2014 at 01:53 PM 0
Share

Hah, I think you commented on the wrong thread :) But I can tell you your problem. If statements need to use comparison operators and not assignment operators (== vs =). (You are also missing the slashes on your comment.)

  //call waitswitch
 StartCoroutine (waitswitch());
 if (dimension1) {
 if (dimension2) {

Because they are booleans you don't need to have any comparison values in the if statement. If's are always asking for true false results. Because you have a boolean you can just go IF (boolean) or IF (!boolean).

Alternatively I believe you can go:

      call waitswitch
     StartCoroutine (waitswitch());
     if (dimension1 == true) {
     if (dimension2 == true) {
 

Also you are missing semi-colons everywhere! (line 6, 8, 23, 24, 26 and 2).

The other problem I see here is you still need to adjust your logic to work correctly. When you call your coroutine you are always setting both dimension variables to true. I don't think this is what you want.

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

21 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

Related Questions

Play two 2D animation together? 2 Answers

Flipping shot point 2d shooter 1 Answer

2D shooter, 3d or 2d colliders? 2 Answers

2D sidescroller shooting 1 Answer

Instantiate gameobject on random position on 1. a graph and 2. another gameobject 0 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