• 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 maggot · Aug 13, 2010 at 02:46 PM · c#shootcharge

Power up weapon shot before releasing and resetting power

What would be the code so that holding down a button will charge up shot,then releasing that button fires the charged shot and resets the charge; as used in games like r-type?

Comment
Add comment · Show 1
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 Peter G · Aug 14, 2010 at 12:13 AM 0
Share

Don't forget to mark the right answer.

1 Reply

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

Answer by Peter G · Aug 13, 2010 at 04:30 PM

I would do something like this. It starts charging when the user clicks the fire button and will release when they let go.

var chargeLevel : float = 0; //Don't change this in the inspector. var chargeSpeed : float = 1; //Default, the charge will go up 1 per second var isCharging = false;

function Update () { if (Input.GetButtonDown("FireButton")) { //Did the user click? if(!isCharging) { //Some what unnecessary due to the way the Input is // setup isCharging = true; CalculateCharge(); } } }

function CalculateCharge () { while(Input.GetButton("FireButton")) { //Add to the charge as long as the // user is holding the button chargeLevel += Time.deltaTime * chargeSpeed; yield; //Will cause a crash without this. }

  //Fire Projectile
  // Reset the vars.
  chargeLevel = 0.0;
  isCharging = false;

}

Comment
Add comment · Show 2 · 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 maggot · Aug 13, 2010 at 05:06 PM 0
Share

Yes, thats what i was getting - crashes with while loops, thanks

avatar image Selocanus · Jul 20, 2020 at 07:08 AM 0
Share

Hey dude thanks for the idea. I appreciate that :thumbs_up:

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Problem creating a 2D scroller shooting game 2 Answers

Adding lights and cameras to scenes with cSharp script 2 Answers

Error trying to createMonoBehaviour using the 'new' keyword in cSharp script 4 Answers

Bullet not moving 1 Answer

Attaching position of an object in game to position of cursor on screen 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