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

2D Shooting problems

Discussion in '2D' started by DanielPrz, Mar 30, 2020.

  1. DanielPrz

    DanielPrz

    Joined:
    Mar 28, 2020
    Posts:
    5
    Hi.

    I´m a very beginner in game development and I´m having big problems with my first project.

    Right now I´m scripting my character´s controls and I´m having some problems with the shooting. The first problem is that the animation for the character when shooting plays really fast and you don´t really get to see it. I´ve tried reducing the fps and nothing.
    The second problem is tha when I click to shoot, it invokes more than one bullet per click. I don´t know why. I´ve watched so many tutorials and I can´t find a solution. Is driving me crazy!

    Any help could be apreciated!
     
  2. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    For the animation you have 2 choice: increase the time between the keyframes or reduce the animation speed. For the second problem it's hard to find what's wrong, you should post the part of code handling that so we can understand
     
  3. DanielPrz

    DanielPrz

    Joined:
    Mar 28, 2020
    Posts:
    5
    This is the code so far for the bullet:

    upload_2020-3-30_12-46-26.png

    For the player, this is the code:

    upload_2020-3-30_12-48-18.png
     
  4. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    You have to check manually to fire with delay. I use this
    Code (CSharp):
    1. public void Shoot() {
    2.  
    3.         if (Time.time > nextFire) {         //Check if can fire again
    4.            
    5.             nextFire = Time.time + fireRate;          //Increase the variable nextFire to allow to have delay between shots
    6.  
    7.             //Fire
    8.            
    9.         }    
    10.  
    11.     }
    12. }
    where Shoot() is called inside the input player script with if (Input.GetButtonDown("Fire1")) and nextFire is just a float
     
  5. DanielPrz

    DanielPrz

    Joined:
    Mar 28, 2020
    Posts:
    5
    Sorry. Too deep xD

    I use those lines with my actual code? and what are "nextFire" and "fireRate"?
     
  6. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    Yes, just replace "//Fire" with your instantiate bullet. nextFire is just for calculating the time, declare it as private float and you are done. fireRate is, well, the fire rate, the time between a bullet and another. It's always a float, set is as public so you can modify it as you prefer. For example if you set it to "1" you will shoot a bullet every 1 second. If you set "2" you will shoot a bullet every 2 seconds
     
  7. DanielPrz

    DanielPrz

    Joined:
    Mar 28, 2020
    Posts:
    5
    Sorry. I was working on something else and dind´t get to answer.

    Thanks for the advise but it didn´t solve it. I don´t know what else to do