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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Continuous Spawn/Attack?

Discussion in 'Scripting' started by AdamEarlston, Nov 14, 2020.

  1. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Edit: I finally found the correct word to google lol, "Repeating", which lead me to this page https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html Hope this helps anyone else who is as bad as me >.<




    Basically when I hold down Left Click, I want the game to keep on shooting until I release left click, but my code only shoots once per click. I seem to be googling the wrong things to find the solution.

    Here's part of the script:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         Vector2 bowPosition = transform.position;
    4.         Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    5.         direction = mousePosition - bowPosition;
    6.         transform.right = direction;
    7.  
    8.         if (timeBtwShots <= 0)
    9.         {
    10.             if (Input.GetMouseButtonDown(0))
    11.             {
    12.  
    13.                 Shoot();
    14.                 timeBtwShots = startTimeBtwShots;
    15.             }
    16.         }
    17.         else
    18.         {
    19.             timeBtwShots -= Time.deltaTime;
    20.         }
    21.  
    22.         for (int i = 0; i < numberOfPoints; i++)
    23.         {
    24.             points[i].transform.position = PointPosition(i * spaceBetweenPoints);
    25.         }
    26.     }
    I've tried switching around the order of the "if"s and placed Shoot(); in different positions but i can't figure it out. I thought Input.GetMouseButtonDown(0) was a continuous thing? Or is the problem that the Shoot(); script isn't set to be continuous?

    Code (CSharp):
    1. void Shoot()
    2.     {
    3.         GameObject newArrow = Instantiate(arrow, shotPoint.position, shotPoint.rotation);
    4.         newArrow.GetComponent<Rigidbody2D>().velocity = transform.right * launchForce;
    5.  
    6.         Destroy(newArrow, 10);
    7.  
    8.     }
    Any help is greatly appreciated <3
     
    Last edited: Nov 14, 2020
  2. NitroZoron

    NitroZoron

    Joined:
    Jun 29, 2020
    Posts:
    42
    Try using
    Code (CSharp):
    1. if (input.getkeydown("mouse1")
    2. {
    3. //code here
    4. }
    5.  
     
  3. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
  4. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    I figured it out thanks, this is what I ended up with (although I think the old part that includes timeBtwShots isn't even being used now):

    Code (CSharp):
    1.         if (timeBtwShots <= 0)
    2.         {
    3.             if (Input.GetMouseButtonDown(0))
    4.             {
    5.                 // Starting in 0.5 seconds.
    6.                 // a projectile will be launched every 1 second
    7.                 InvokeRepeating("Shoot", 0.5f, 1.0f);
    8.                 timeBtwShots = startTimeBtwShots;
    9.             }
    10.         }
    11.         else
    12.         {
    13.             timeBtwShots -= Time.deltaTime;
    14.         }
    15.  
    16.         for (int i = 0; i < numberOfPoints; i++)
    17.         {
    18.             points[i].transform.position = PointPosition(i * spaceBetweenPoints);
    19.         }
    20.         if (Input.GetMouseButtonUp(0))
    21.         {
    22.             CancelInvoke();
    23.         }
    24.     }
    InvokeRepeating on GetButtonDown and CancelInvoke on GetButtonUp are the 2 biggies that solved it.

    Here's a video of it working lol

    I was too lazy to add a bow animation to the game (for now anyway), so I used magic to control the bow >.<