How to shoot bullets depending on the players position???,shooting bullets depending on players x position

im new to coding and unity2D in general and ive been trying to figure this out for a while now.
i know there are lots of tutorials on how to shoot bullets, but not with this type of specific script ive wrote. any help would be appreciative! heres my script…

[SerializeField] float runSpeed = 5f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float projetileSpeed = 10f;

[SerializeField] Transform groundCheck;
[SerializeField] Transform groundCheckL;
[SerializeField] Transform groundCheckR;

[SerializeField] GameObject bullet;
[SerializeField] Transform bulletSpawnPos;



Animator animator;
Rigidbody2D rb;
SpriteRenderer spriterenderer;

bool isGrounded;



// Start is called before the first frame update
void Start()
{
    animator = GetComponent<Animator>();
    rb = GetComponent<Rigidbody2D>();
    spriterenderer = GetComponent<SpriteRenderer>();
}

private void Update()
{
    Run();
    FlipSprite();
    Jump();
    Shoot();
}

private void FixedUpdate()
{
    if ((Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"))) ||
        (Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Ground"))) ||
        (Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Ground"))))

    {
        isGrounded = true;
    }
    else
    {
        isGrounded = false;
        animator.Play("Jump");
    }
    
}

private void Run()
{
    float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
    Vector2 PlayerVelocity = new Vector2(controlThrow * runSpeed, rb.velocity.y);
    rb.velocity = PlayerVelocity;
    bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
    if(isGrounded)
        animator.SetBool("Run", playerHasHorizontalSpeed);

 
}

private void Jump()
{
    if (CrossPlatformInputManager.GetButtonDown("Jump") && isGrounded)
    {
        Vector2 jumpVelocityToAdd = new Vector2(0, jumpSpeed);
        rb.velocity = rb.velocity + jumpVelocityToAdd;

        if(isGrounded)
            animator.Play("Jump");

    }
  
}

private void Shoot()
{
    if (Input.GetKeyDown(KeyCode.Mouse0)){
        animator.Play("Shoot");

            GameObject BULLET = Instantiate(
            bullet,
            bulletSpawnPos.transform.position,
             Quaternion.identity) as GameObject;

        BULLET.GetComponent<Rigidbody2D>().velocity = new Vector2(projetileSpeed, 0f);
     
      
    }
    
}

private void FlipSprite()
{
    bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
    if (playerHasHorizontalSpeed)
    {
        transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);
    

    }
 
}

},i recently started learning c# and unity and ive been stuck trying to figure this out :frowning:
My player only shoots right when facing right, but still shoots right when facing left.
Lots of tutorials out there but not with the specific script ive built.

If anyone can chime in and help it would be appreciative! heres my script!!!

SerializeField] float runSpeed = 5f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float projetileSpeed = 10f;

[SerializeField] Transform groundCheck;
[SerializeField] Transform groundCheckL;
[SerializeField] Transform groundCheckR;

[SerializeField] GameObject bullet;
[SerializeField] Transform bulletSpawnPos;



Animator animator;
Rigidbody2D rb;
SpriteRenderer spriterenderer;

bool isGrounded;



// Start is called before the first frame update
void Start()
{
    animator = GetComponent<Animator>();
    rb = GetComponent<Rigidbody2D>();
    spriterenderer = GetComponent<SpriteRenderer>();
}

private void Update()
{
    Run();
    FlipSprite();
    Jump();
    Shoot();
}

private void FixedUpdate()
{
    if ((Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"))) ||
        (Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Ground"))) ||
        (Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Ground"))))

    {
        isGrounded = true;
    }
    else
    {
        isGrounded = false;
        animator.Play("Jump");
    }
    
}

private void Run()
{
    float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
    Vector2 PlayerVelocity = new Vector2(controlThrow * runSpeed, rb.velocity.y);
    rb.velocity = PlayerVelocity;
    bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
    if(isGrounded)
        animator.SetBool("Run", playerHasHorizontalSpeed);

 
}

private void Jump()
{
    if (CrossPlatformInputManager.GetButtonDown("Jump") && isGrounded)
    {
        Vector2 jumpVelocityToAdd = new Vector2(0, jumpSpeed);
        rb.velocity = rb.velocity + jumpVelocityToAdd;

        if(isGrounded)
            animator.Play("Jump");

    }
  
}

private void Shoot()
{
    if (Input.GetKeyDown(KeyCode.Mouse0)){
        animator.Play("Shoot");

            GameObject BULLET = Instantiate(
            bullet,
            bulletSpawnPos.transform.position,
             Quaternion.identity) as GameObject;

        BULLET.GetComponent<Rigidbody2D>().velocity = new Vector2(projetileSpeed, 0f);
     
      
    }
    
}

private void FlipSprite()
{
    bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
    if (playerHasHorizontalSpeed)
    {
        transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);
    

    }
 
}

}

Hey there,
The problem is in code line 96:

BULLET.GetComponent<Rigidbody2D>().velocity = new Vector2(projetileSpeed, 0f);

You are changing the bullet’s velocity in x direction without regarding the player’s direction. As your projectileSpeed is positive, the bullet will always start to the right. I guess the best solution is to have a variable store the direction you are currently heading, set in your Run() function.
According to the Unity manual, changing velocity of an object directly is not recommended, so maybe you would like to switch that to addForce(). You can find an example for launching a projectile in this tutorial, just in case you would like to read into this a little bit further :slight_smile: