• 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 Blast73 · Dec 22, 2015 at 03:15 AM · c#shooting

Shooting a projectile the longer a button is held c#

I'm trying to shoot a projectile the longer the mouse is held down. To do this I am incrementing a variable in void update. I seemingly have two problems. 1. The variable is not properly increasing, and 2. nothing is happening when GetButtonUp should be called. Here is my code so far:

EDIT: I fixed problem 2. I was calling the buttonUp on "Fire2" not "Fire1." The first problem still persist.

  GameObject prefab;

 // Use this for initialization
 void Start () {
     prefab = Resources.Load ("Projectile") as GameObject;

 }
 
 // Update is called once per frame
 void Update () {
 
     int x = 0;

     if (Input.GetButton ("Fire1")) {

         //Debug.Log ("Mouse 1 down");

         x += 1;

         Debug.Log (x);

         //if (x < 99) 
             //x = 100;

         if (Input.GetButtonUp ("Fire2")) {
             Debug.Log ("Mouse 1 up");
             GameObject Projectile = Instantiate (prefab, transform.position, transform.rotation)                      as GameObject;
             Projectile.transform.position = transform.position + Camera.main.transform.forward   * 2;
             Rigidbody rb = Projectile.GetComponent<Rigidbody> ();
             rb.velocity = Camera.main.transform.forward * (x / 4);

         }        
     }
 }
 }
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 Bunny83 · Dec 22, 2015 at 04:12 AM

You have 3 problems here:

  • You use an int variable where you should use a float variable that represents the "time passed" instead of frames passed.

  • You declared your variable inside Update. That means it will loose it's content after Update is completed. Since it should hold it's value over multiple frames you have to declare it outside of Update as a class member variable.

  • GetButtonUp will only return true for one frame when the button is released. Since you have the check inside the other if that will never happen. If the button was released this frame then GetButton will return false so you don't enter the outer if anymore.

So it should look like that:

 public float minForce = 0.2f;
 public float maxForce = 2f;
 public float chargeSpeed = 1f;
 float x = 0f;
 
 void Update ()
 { 
     if (Input.GetButton ("Fire1"))
     {
         x += Time.deltaTime * chargeSpeed; // increase x by "chargeSpeed" per second
     }
     if (Input.GetButtonUp ("Fire1"))
     {
         GameObject Projectile = Instantiate (prefab, transform.position, transform.rotation)                      as GameObject;
         Projectile.transform.position = transform.position + Camera.main.transform.forward   * 2;
         Rigidbody rb = Projectile.GetComponent<Rigidbody> ();
         x = Mathf.Clamp(x, minForce, maxForce);
         rb.velocity = Camera.main.transform.forward * x;
         x = 0f;
     }        
 }

minForce allows you to specify a minimum force that is used if the user only "clicks".

maxForce limits the force you can reach.

chargeSpeed controls how fast your variable charges.

Once the projectile is launched it's important to set x back to 0 for the next one.

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 Blast73 · Dec 22, 2015 at 05:42 AM 0
Share

Thanks for the help. I got my system to work by placing the variable outside of the update but I will use yours. The way you set it up allows for more precise control and having the charge based on time is a big plus.

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

42 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I fix shooting position? 1 Answer

Shooting controls 1 Answer

How to Sync bullet spawn with shoot animation 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