When player shoot a butllet, the bullet does not go straight at what the player is facing.

Hello, I am trying to shoot without using mouse position. My player rotate to the right o the left. The player is the middle center of the screen in a fixed point. in the middle of the screen. When the player shoot the bullet goes up. If I change the script, the bullet goes up and right or left but never goes at what the player is facing.

here is the code I have.

PlayerScript.c#
[SerializeField] GameObject bulletPrefab;
[SerializeField] GameObject bulletPosition;

private void Rotate(float rotation) // rotate to the left or right
{
transform.rotation = Quaternion.Euler(Vector3.forward * rotation);
}

void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, bulletPosition.transform.position, bulletPosition.transform.rotation) as GameObject;
bullet.GetComponent().velocity = new Vector2(0, 1) * 5f;
}

I had try :
-AddRelativeForce
-AddForce

But nothing works. The most near script that works for me is
Vector2 direction = new Vector2(bulletPosition.transform.position.x, bulletPosition.transform.position.y);
direction.x *= -1;
Rigidbody2D rb = bullet.GetComponent();
rb.velocity = (direction * -1) * speed;

but still does not work properly.

Thanks.

GameObject bullet = Instantiate(bullet, transform.position, transform.rotation)
bullet.GetComponent().velocity = bullet.transform.forward * speed;

this script should be apply to the camera

Thank you, I solved this issue. I have to add a bullet script to the bullet itself. That works super fine for me.