I'm having trouble with the shooting system.I'm having trouble with the firing system. (2D)

,

Hello, I have a problem with the firing system, as the bullet stays still, or moves wrongly. I’m using a rigidbody2D on the bullet, but the force doesn’t seem to be applied and therefore doesn’t move.

This is a part of the script, it’s the moment when it’s created.

private void Disparar()
    {
        GameObject bala;
        bala = Instantiate(municion, bocaArmaFusil.position, bocaArmaFusil.rotation);
        bala.GetComponent<Rigidbody2D>().AddForce(bocaArmaFusil.forward * fuerzaInicial);
        cantidadBalasFusil--;
    }

I already found the solution, it is basically in two scripts:

  • One in the weapon: in this instance a prefabricated or prefab of the bullet.
  • The other one goes in the bullet and this one goes like this:
    public Rigidbody2D rb;
    public float velocidad = 0;

    void Start()
    {
        rb.velocity = transform.right * velocidad;
    }

This is suitable for 2D platform games. Example the mario bros