Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how do i fix shooting,when I shoot diagonally it also shoots 2 extra bullets in horizontal and vert.

Discussion in 'Getting Started' started by sahilsharma220202004, May 13, 2021.

  1. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Shooting : MonoBehaviour
    6. {
    7.     [SerializeField] float bulletForce;
    8.     [SerializeField] float waitTime;
    9.     public bool isShooting = false;
    10.  
    11.     public GameObject bulletPrefab;
    12.     private Animator animator;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         animator = GetComponent<Animator>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         // For Diagonal Shooting & animating
    24.         if (Input.GetKey(KeyCode.UpArrow) && Input.GetKeyDown(KeyCode.RightArrow))
    25.         {
    26.             Shoot(new Vector2(1, 1));
    27.             animator.SetFloat("Vertical", 1);
    28.             animator.SetFloat("Horizontal", 0);
    29.         }
    30.         else if (Input.GetKey(KeyCode.UpArrow) && Input.GetKeyDown(KeyCode.LeftArrow))
    31.         {
    32.             Shoot(new Vector2(-1, 1));
    33.             animator.SetFloat("Vertical", 1);
    34.             animator.SetFloat("Horizontal", 0);
    35.         }
    36.         else if (Input.GetKeyDown(KeyCode.LeftArrow) && Input.GetKey(KeyCode.DownArrow))
    37.         {
    38.             Shoot(new Vector2(-1, -1));
    39.             animator.SetFloat("Vertical", -1);
    40.             animator.SetFloat("Horizontal", 0);
    41.         }
    42.         else if (Input.GetKeyDown(KeyCode.RightArrow) && Input.GetKey(KeyCode.DownArrow))
    43.         {
    44.             Shoot(new Vector2(1, -1));
    45.             animator.SetFloat("Vertical", -1);
    46.             animator.SetFloat("Horizontal", 0);
    47.         }
    48.  
    49.         // Normal Shooting & animating
    50.         else if (Input.GetKeyDown(KeyCode.UpArrow))
    51.         {
    52.             Shoot(Vector2.up);
    53.             animator.SetFloat("Vertical", 1);
    54.             animator.SetFloat("Horizontal", 0);
    55.         }
    56.         else if (Input.GetKeyDown(KeyCode.DownArrow))
    57.         {
    58.             Shoot(Vector2.down);
    59.             animator.SetFloat("Vertical", -1);
    60.             animator.SetFloat("Horizontal", 0);
    61.         }
    62.         else if (Input.GetKeyDown(KeyCode.RightArrow))
    63.         {
    64.             Shoot(Vector2.right);
    65.             animator.SetFloat("Horizontal", 1);
    66.             animator.SetFloat("Vertical", 0);
    67.         }
    68.         else if (Input.GetKeyDown(KeyCode.LeftArrow))
    69.         {
    70.             Shoot(Vector2.left);
    71.             animator.SetFloat("Horizontal", -1);
    72.             animator.SetFloat("Vertical", 0);
    73.         }
    74.     }
    75.  
    76.     void Shoot(Vector2 dir)
    77.     {
    78.         isShooting = true;
    79.         animator.SetBool("isShooting", true);
    80.  
    81.         GameObject bullet = Instantiate(bulletPrefab, transform.position, bulletPrefab.transform.rotation);
    82.         Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();
    83.         bulletRb.AddForce(dir.normalized * bulletForce, ForceMode2D.Impulse);
    84.  
    85.         // Changes isShooting to false after waitTime
    86.         StartCoroutine(waitForSomeTime());
    87.     }
    88.  
    89.     // Changes animator's isShooting bool state
    90.     IEnumerator waitForSomeTime()
    91.     {
    92.         yield return new WaitForSeconds(waitTime);
    93.         isShooting = false;
    94.         animator.SetBool("isShooting", false);
    95.     }
    96. }
     
  2. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    I believe it is because multiple if statements are true. If you hit Up Arrow and Left Arrow, the else if at line 30 is true, but the if statement at line 50 is also True.
     
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    That whole chain is one big if-else, so only one of them will ever be true on any given frame.

    I think what's probably happening is that there's nothing preventing the shooting from happening on the frames where you're only holding down one of the two keys. Unless you're some kind of crazy-coordinated hotshot, it's unlikely you'll be able to press two keys exactly at the same time. So there is at least a frame or two where you're just pressing one of the keys, and the game thinks you want to shoot in one of the cardinal directions. Similarly, as you're releasing the keys, your fingers probably lift off one of them first, if only for a split second, which makes the game think you're trying to shoot in a cardinal direction again.

    This is at least partially happening because your shooting logic is frame rate dependent. If your game is running at 60fps, you'll be able to shoot 60 bullets a second. If it runs at 120fps, it'll be 120 bullets a second. I would think you could fix that and potentially solve your directional issue by using your method only to track when and where the player wants to shoot, and passing that information into a system that keeps track of time and the player's allowed rate of fire to determine when to actually shoot bullets.
     
  4. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    Sir, actually I did try a lot of things .
    Actually I started learning unity about 2 months ago and completed 2 parts of junior programmer course
    So it feels very tough to make systems at this point, can u pls send something on how to make it ;(
     
  5. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    I have done some research and made this system,
    it does improve shooting but still sometimes it does the wrong thing.
    any improvements??
    Code (CSharp):
    1. void Update()
    2.     {
    3.         //Get Input and shoot based on input direction after fixed time
    4.         GetInput();
    5.         timeCount += Time.fixedDeltaTime;
    6.         if (shootDir != new Vector2(0, 0) && timeCount > fireRate)
    7.         {
    8.             Shoot(shootDir);
    9.             timeCount = 0;
    10.         }
    11.     }
    12.  
    13.     // Gets Input key and gives shoot direction based on it
    14.     void GetInput()
    15.     {
    16.         if (Input.GetKey(KeyCode.UpArrow))
    17.         {
    18.             if (Input.GetKey(KeyCode.LeftArrow))
    19.             {
    20.                 shootDir = new Vector2(-1, 1);
    21.             }
    22.             else if (Input.GetKey(KeyCode.RightArrow))
    23.             {
    24.                 shootDir = new Vector2(1, 1);
    25.             }
    26.             else
    27.             {
    28.                 shootDir = Vector2.up;
    29.             }
    30.         }
    31.  
    32.         else if (Input.GetKey(KeyCode.LeftArrow))
    33.         {
    34.             if (Input.GetKey(KeyCode.UpArrow))
    35.             {
    36.                 shootDir = new Vector2(-1, 1);
    37.             }
    38.             else if (Input.GetKey(KeyCode.DownArrow))
    39.             {
    40.                 shootDir = new Vector2(-1, -1);
    41.             }
    42.             else
    43.             {
    44.                 shootDir = Vector2.left;
    45.             }
    46.         }
    47.  
    48.         else if (Input.GetKey(KeyCode.RightArrow))
    49.         {
    50.             if (Input.GetKey(KeyCode.UpArrow))
    51.             {
    52.                 shootDir = new Vector2(1, 1);
    53.             }
    54.             else if (Input.GetKey(KeyCode.DownArrow))
    55.             {
    56.                 shootDir = new Vector2(1, -1);
    57.             }
    58.             else
    59.             {
    60.                 shootDir = Vector2.right;
    61.             }
    62.         }
    63.  
    64.         else if (Input.GetKey(KeyCode.DownArrow))
    65.         {
    66.             if (Input.GetKey(KeyCode.LeftArrow))
    67.             {
    68.                 shootDir = new Vector2(-1, -1);
    69.             }
    70.             else if (Input.GetKey(KeyCode.RightArrow))
    71.             {
    72.                 shootDir = new Vector2(1, -1);
    73.             }
    74.             else
    75.             {
    76.                 shootDir = Vector2.down;
    77.             }
    78.         }
    79.  
    80.         else
    81.         {
    82.             shootDir = new Vector2(0, 0);
    83.         }
    84.     }