Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 FairySeraph · Aug 19, 2017 at 05:24 PM · shootingshotfiringcharge

Coding A Megaman-like Charge Shot?

So, as the title implies, I'm trying to code a charged shot for a space shooting game I'm making for practice. Nothing special, just trying to set parameters to where the shot will only be fired if the button is held down for more than two seconds. Problem is, the shot won't fire, and I don't know what I did wrong. I tried to set it to a timer system, but so far, I haven't had any luck. I separated the shot's scripting into two parts: one merely controls the direction it goes as well as speed, which is only used to make the shot a prefab, and the other is supposed to define how it fires. Here is the script for firing the shot:

 public Transform chargeSpawn;
 public GameObject chargeShot;
 private float chargeTime = 0;
 private float chargeRate = 2f;
 public float fireRate1;
 public float nextFire1;

 void Start ()
 {
     StartCoroutine(TimerRoutine());
 }

 IEnumerator TimerRoutine()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         yield return new WaitForSeconds(2f);
         chargeTime += chargeRate;
     }
     if (Input.GetButtonUp("Fire1") && Time.time > 2f)
     {
         Instantiate(chargeShot, chargeSpawn.transform.position, chargeSpawn.transform.rotation);
         GetComponent<AudioSource>().Play();
         chargeTime = 0;
     }
     if (Input.GetButtonUp("Fire1") && Time.time < 2f)
     {
             chargeTime = 0;
     }
 }

I'm sure it's something simple I've overlooked, but, hey. And I've really not seen much discussion regarding charged shots. Hopefully someone else as green as I am can get some help out of this, too.

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

1 Reply

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

Answer by yaezah · Aug 19, 2017 at 06:47 PM

I'm still a noob so my code might be a little off, but try putting it in an update function and adding a bool to check if you can shoot or not so it doesn't shoot every frame, I sense that it's not shooting for you because it's only being called once in the start function..

     public Transform chargeSpawn;
     public GameObject chargeShot;
     private float chargeTime = 0;
     private float chargeRate = 2f;
     public float fireRate1;
     public float nextFire1;
     public bool canShoot = false;
 
     void Update ()
     {
         if (!canShoot) {
             StartCoroutine (TimerRoutine ());
         }
     }
 
     IEnumerator TimerRoutine()
     {   canShoot = true;
             if (Input.GetButtonDown("Fire1"))
             {
                 yield return new WaitForSeconds(2f);
                 chargeTime += chargeRate;
             }
             if (Input.GetButtonUp("Fire1") && Time.time > 2f)
             {
                 Instantiate(chargeShot, chargeSpawn.transform.position, chargeSpawn.transform.rotation);
                 GetComponent<AudioSource>().Play();
                 chargeTime = 0;
             }
             if (Input.GetButtonUp("Fire1") && Time.time < 2f)
             {
                 chargeTime = 0;
             }
         canshoot = false;
             }
 
 }
Comment
Add comment · Show 7 · 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 FairySeraph · Aug 19, 2017 at 07:17 PM 1
Share

Oh my god, that's all it was! I made the mistake of using Start ins$$anonymous$$d of Update. It woks just fine now! Thanks, man. I really appreciate the help.

avatar image yaezah FairySeraph · Aug 19, 2017 at 08:12 PM 0
Share

Glad it worked for you :) Probably didn't even require the bool either right? I just added it because I had the same problem yesterday but when I held mouse it would instantiate a prefab EVERY frame because it was in update xD

avatar image FairySeraph yaezah · Aug 19, 2017 at 08:53 PM 0
Share

Actually, I did encounter another problem. It's fairly simple, though. Once the coroutine has finished, it doesn't continue to work after the first shot. Basically, the first one waits for the amount of time to pass before firing, but after that, they all fire as soon as the button is let go. I believe if I just manage to loop the coroutine that starts the timer, it should be solved. How should I go about making the coroutine repeat every time the button is held down?

Show more comments

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

69 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 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 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 avatar image avatar image avatar image

Related Questions

Making Coroutine Repeat? 3 Answers

DmC: Devil May Cry Dual Shot? 0 Answers

Issues with bullet not subtracting properly 1 Answer

Problem with a charged projectile direction 1 Answer

Can't move and fire at the same time! 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