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. Dismiss Notice

Question 2D Shooter bullets don't render properly

Discussion in '2D' started by MarkElement, Aug 17, 2023.

  1. MarkElement

    MarkElement

    Joined:
    Jan 2, 2023
    Posts:
    12
    Hi all,

    First post here and also very early days teaching myself unity so please bare with me.

    I'm using the new input system. I have a weaponSprite, a firePoint sprite and a bullet prefab.
    I have a playerMovement, which is attached to my player sprite. Inside of which I have a:

    public InputAction playerShoot
    In the Inspector i've mapped this to my Gamepads "right trigger"

    I also have a:

    [Serialized] public Weapon weapon = new Weapon();


    which has my weapon.cs attached to it

    My Update() method contains the following:
     playerShoot.performed += _ => weapon.Fire();

    which i borrowed from here

    And finally my weapon.cs's Fire() method looks like this:

    Code (CSharp):
    1.     public void Fire()
    2.     {
    3.         GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    4.         bullet.GetComponent<Rigidbody2D>().AddForce(firePoint.up * 1000, ForceMode2D.Impulse);
    5.     }
    The problem i have is when i press the right trigger, i see in the hirachy my bullets are spawning very quickly and dissappearing (this is fine, i'm destroying the object using OnBecameInvisible. but i can't actually see the bullets properly, the seem to glitch - I feel like maybe i'm rendering them too fast or something?

    I've attached a gif showing what my issue is, as you can see the black bullet spawns on the firePoint but doesn't seem to render the animation of force


    If there's any more information I 2023-08-17 14-22-53.gif can provide just ask, thanks !
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    You're trying to move the bullet at 1000m/s which is crazy fast. I would suggest that you need to scale down the size of your visuals if you're moving stuff that fast.
     
  3. MarkElement

    MarkElement

    Joined:
    Jan 2, 2023
    Posts:
    12
    AHA, thanks i didn't make that connection, setting it to 20f gives me a more reasonable bullet speed now thanks!

    Next question if you don't mind, what would be the correct approach to introduce rate of fire? I'm assuming some kind of delay bettween spawning each bullet. I know dotnet has thread.sleep for example but i don't see anything similar in mono?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    MelvMay likes this.
  5. MarkElement

    MarkElement

    Joined:
    Jan 2, 2023
    Posts:
    12
    Kurt-Dekker likes this.