How to fire a gun in 2d game

i want to make the bullet go to left side when player turn left and go right when player turn right.

thank you

#Javascript

I’m thinking that when you press left key the character will rotate 180 degrees if you were facing right, so, first parent the gun to your gameobject, and create a bullet spawner, when the character rotates, the spawner should be too, and the ray(if you’re using raycast) or the object(rigidbody bullets) should be spawned to the right way, if this doesn’t work I just came up with something:
something like this, btw it may ahve some errors since I’m writing them directly here and not testing them:

    var damage : int; //set this to wathever you want to be the damage
    var maxDistance : float;
                var distance : float;
                var bulletSpawner : GameObject;//Create a empty game object and put this in the part where the shot goes of the gun
                var turnedLeft : boolean = false;
                var turnedRight : boolean = true; // this assumes that your character starts turned right, if he starts turned left change them on the inspector or in the script itself
                
                function Update()
                {
                    if (Input.GetButtonDown("left"))//create button left and right on input settings
                    turnedLeft = true;
                    turnedRight = false;
                    if (Input.GetButtonDown("right"))
                    turnedRight = true;
                    turnedLeft = false;
                
                    if (turnedRight && Input.GetButtonDown("Fire1"))//assuming that you use mouse 1 to shoot
                  {
                      var var hit : RaycastHit;
                      if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.right), hit))
            {
                Distance = hit.distance;
            	if (Distance < MaxDistance)
                {
                 hit.transform.SendMessage("ApllyDamage", damage, SendMessageOptions.DontRequireReceiver);
                }
            }
          }
if (turnedLeft && Input.GetButtonDown("Fire1"))//assuming that you use mouse 1 to shoot
                  {
                      var var hit : RaycastHit;
                      if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.left), hit))
            {
                Distance = hit.distance;
            	if (Distance < MaxDistance)
                {
                 hit.transform.SendMessage("ApllyDamage", damage, SendMessageOptions.DontRequireReceiver);
                }
            }
          }
            
     }

Ok I think we’re done, well we don’t have fire speed, now it’s create function ApllyDamage on the enemies script and say something like this:

var health : int = 100;
    
function ApplyDamage (damage : int)
{
  health -= damage;
}

function Update()
{
 if (health <=0)
 destroy(gameObject)
}

forgive for any errors, I just tried to help, probably have one problem. I putted to shoot left when turned left and right when turned right, that would probably have problems since when you are turned left, you’re left is like the camera view if you get what I mean. Anyway try the way I wrote if it doesn’t work change every raycast thingies to forward like this:
(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward), hit))

Now it’s getting clearer, your velocity variable is set to -20 so when the player’s facing left it goes left but when the player’s facing right it should get set to +20 since it’s a 2d game & the “c” is the shooting button I assume your movement controls are the arrows so let’s try this:

var velocity : float;
function Update(){
rigidbody.velocity.x = velocity;
if(input.getkey(KeyCode.leftarrow)){
velocity = - 20;}
if(input.getkey(KeyCode.rightarrow)){
velocity = 20;
}
}

I guess this should do it.