Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I make an arrow charge the longer I hold a button in my script?

Discussion in 'Scripting' started by adamhood15, Jul 10, 2022.

  1. adamhood15

    adamhood15

    Joined:
    Jul 4, 2022
    Posts:
    3
    As the title says, I am trying to add a feature to my game that allows the player to hold down the shoot button and charge their arrow so that it has more power and flies further. If you tap the shoot button it will fire but will not cause much damage or go very far.

    Here is my code that I have currently. I am very new to coding and unity in general so any explanation you can provide for your solution would be awesome.

    Code (CSharp):
    1. public class Bow : MonoBehaviour
    2. {
    3. public GameObject arrow;
    4. public float launchForce;
    5. public Transform shotPoint;
    6.  
    7.  
    8. public GameObject point;
    9. GameObject[] points;
    10. public int numberOfPoints;
    11. public float spaceBetweenPoints;
    12. Vector2 direction;
    13.  
    14.     private void Start()
    15.     {
    16.         points = new GameObject[numberOfPoints];
    17.         for (int i = 0; i < numberOfPoints; i++)
    18.         {
    19.             points[i] = Instantiate(point, shotPoint.position, Quaternion.identity);
    20.         }
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         //bow rotation to track mouse
    26.         Vector2 bowPosition = transform.position;
    27.         Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    28.         direction = mousePosition - bowPosition;
    29.         transform.right = direction;
    30.  
    31.         if (Input.GetMouseButtonDown(0))
    32.         {
    33.                 Shoot();
    34.         }
    35.  
    36.         for (int i = 0; i < numberOfPoints; i++)
    37.         {
    38.             points[i].transform.position = PointPosition(i * spaceBetweenPoints);
    39.         }
    40.     }
    41.  
    42. //Shoot method
    43. void Shoot(){
    44.     GameObject newArrow = Instantiate(arrow, shotPoint.position, shotPoint.rotation);
    45.     newArrow.GetComponent<Rigidbody2D>().velocity = transform.right * launchForce;
    46.    
    47.  
    48.  
    49. }
    50. //aim tracker
    51. Vector2 PointPosition(float t) {
    52.     Vector2 position = (Vector2)shotPoint.position + (direction.normalized * launchForce * t) + 0.5f * Physics2D.gravity * (t * t);
    53.     return position;
    54. }
    55. }
     
  2. adamhood15

    adamhood15

    Joined:
    Jul 4, 2022
    Posts:
    3
    Also! I want to modify my script to begin charging when I press the mouse button down but won't fire until you release the mouse button up.
     
  3. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    How about
    Code (CSharp):
    1. int framesSinceCharging = 0;
    2. enum ShootType {Weak, Normal, Strong}
    3. ShootType shootType;
    4. if (Input.GetMouseButtonDown(0)) {
    5.     framesSinceCharging++;
    6. }
    7. if (Input.GetMouseButtonUp(0)) {
    8.     if (framesSinceCharging > 300) {shootType = ShootType.Strong;}
    9.     else if (framesSinceCharging > 200) {shootType = ShootType.Normal;}
    10.     else {shootType = ShootType.Weak;}
    11.     Shoot(shootType);
    12.     framesSinceCharging = 0;
    13. }
    Personally, I'd rather use two time stamps or a Coroutine and WaitForSeconds instead of the Update to make it frame rate independent, though.
     
  4. adamhood15

    adamhood15

    Joined:
    Jul 4, 2022
    Posts:
    3
    So I tried to input the code but kept getting compiler errors. I'm assuming since you said it was frame rate independent that it wouldn't be in the update function correct?
     
  5. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    I haven't checked the code for spellibg or if it would compile, I just suggested that way as a concept. You may need to fix things in your original code to make it work (e.g. "Shoot" needs to use a ShootType as an argument and make use of it (e.g making the arrow fly further)).