• 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 /
  • Help Room /
avatar image
0
Question by Jaimieself · Mar 10, 2018 at 04:14 PM · projectile

How do i make an arrow charge the longer i hold the fire button?

I have a simple system where when i release the mouse button i spawn in an arrow and it shoots forward (or to the raycast hit point).

Now i want to add in a system where the power (or speed) of the arrow is increased depending on how long you hold the mouse button up to a certain value. In a similar way to games such as Skyrim.

My guess is that i need to have a base power that is multiplied by a value that increases over time when the button is held down.

I have tried a bunch of things and nothing i can do works as i am still fairly new to coding.

Here is the script i'm using to fire the arrow:

 public Rigidbody arrow ;
 public float power = 1500f ;
 public int maxAmmo = 100;
 public int currentAmmo;

 void Start ()
 {
     currentAmmo = maxAmmo;
 }

 void Update () 

 {

     if (currentAmmo > 0) {

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

             Rigidbody instance = Instantiate (arrow, transform.position,
                                      transform.rotation) as Rigidbody; 

             Vector3 fwd = transform.TransformDirection (Vector3.forward);
             instance.AddForce (fwd * power);

             currentAmmo--;

         Debug.Log (currentAmmo);

     }
             
 }

}

}

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 meat5000 · Mar 10, 2018 at 06:40 PM

Something like this? I changed the 'FireButton' in the script to Keyboard J just for testing. Change it to whatever you like.

  float barrelClearance = 2.0f;
  float chargeTimer = 0.0f;
  float chargeTimeMax = 5.0f;
  //int currentAmmo = 10; //DUMMY
  void Update () 
  {
      if (currentAmmo > 0)
      {
          if(Input.GetKey(KeyCode.J))
          {
              chargeTimer += Time.deltaTime;
              if (chargeTimer >= chargeTimeMax)
              {
                  Fire(chargeTimeMax);
                  chargeTimer = 0.0f;
              }
          }
          else if (Input.GetKeyUp (KeyCode.J)) 
          {
              Fire(chargeTimer);
              chargeTimer = 0.0f;
          }
      }         
  }
  
      //public Rigidbody arrow; //DUMMY
      //float power = 100.0f; //DUMMY
      void Fire(float scaler)
      {
          Vector3 transPosAdjust = new Vector3(0.0f, 0.0f, barrelClearance); //(transform.position.x, transform.position.y, transform.position.z + barrelClearance);
          Rigidbody instance = Instantiate (arrow, transform.TransformPoint(transPosAdjust), transform.rotation) as Rigidbody; 
          instance.AddRelativeForce(new Vector3(0.0f,0.0f, power * scaler), ForceMode.Impulse);//(fwd * power * scaler);
          currentAmmo--;
          Debug.Log (power * scaler);
      }
  }

It should go like this : Pressing and holding the button begins a timer. If you exceed max, it fires and sets the 'scaler' to max. If you let go before max it Fires and the scaler is equal to charge timer. You can scale this however you wish, its just for illustration.

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 Jaimieself · Mar 10, 2018 at 08:21 PM 0
Share

Ok iv got it working quite well now and i'm just playing about with the numbers.

The problem now is that if i have a collider on my arrow and mark it as convex it comes out at a weird angle and of course i want it to fire straight like an arrow. Not sure why its doing that.

avatar image meat5000 ♦ Jaimieself · Mar 10, 2018 at 08:28 PM 0
Share

I changed the code again. This version works sweet as a nut. $$anonymous$$y DU$$anonymous$$$$anonymous$$Y variables are just what I added to test. Note I changed input to $$anonymous$$ey J. Added a 'clearance' to stop Arrow sticking on object when fired. I have gone with Relative Force. You can see how simplified it becomes; as I mentioned in the last question ;)

Just to add a little note to your 'Co$$anonymous$$g out at a funny angle' comment. This is simply due to the instantiated body hitting the collider of the player or weapon objects. Either add clearance (as I have added in the code) or go that extra step : Use the Layering system in conjunction with 'Collision $$anonymous$$atrix' to allow or disallow collisions based on layers. Use IgnoreCollision $$anonymous$$ethod to force ignorance between two particular colliders. I suspect the first $$anonymous$$atrix is the best option.

~

https://docs.unity3d.com/$$anonymous$$anual/LayerBasedCollision.html

avatar image Jaimieself meat5000 ♦ · Mar 10, 2018 at 08:42 PM 0
Share

Wow, ok that works really well now, thank you.

avatar image meat5000 ♦ Jaimieself · Mar 10, 2018 at 09:17 PM 0
Share

$$anonymous$$ost welcome. Click Accept under the answer to mark the Chosen solution.

avatar image Jaimieself meat5000 ♦ · Mar 10, 2018 at 09:56 PM 0
Share

Sorry to bother you again but i have a question.

The barrel clearance makes the arrow instantiate for + 2 in the Z direction but that is the global Z direction so when the character turn the arrow comes from the left, right or behind the character. Is there a way to make the barrel clearance local rather than global?

Iv got the layer thing working aswell now so thats good.

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.

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

129 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 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

transform.forward problem 2 Answers

Automatic cannon aiming 2 Answers

2D Top down projectile help (not moving) 0 Answers

Firing cannons at variable angles 1 Answer

Changing a projectile's angle ? 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