Using Vector3 to make a bullet shoot in the direction the player is facing

I’m creating a top down shooter. In my update function, I control the camera and player movement. I also have a function that shoots. However, I’m having trouble getting the bullets to come straight out of the point I have set on the tip of the weapon, called bulletSpawnPoint. It’ll shoot straight if the player is facing straight, but shoots backwards when facing left, right, or backwards.

    void Update()
{
    //Player face mouse
    Plane playerPlane = new Plane(Vector3.up, transform.position);
    Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
    float hitDist = 0.0f;

    if(playerPlane.Raycast(ray, out hitDist))
    {
        Vector3 targetPoint = ray.GetPoint(hitDist);
        Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
        targetRotation.x = 0;
        targetRotation.z = 0;
        playerObj.transform.rotation = Quaternion.Slerp(playerObj.transform.rotation, targetRotation, 7f * Time.deltaTime);
    }
    //Player Movement
    if (Input.GetKey(KeyCode.W))
    {
        transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.A))
    {
        transform.Translate(Vector3.left * movementSpeed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.S))
    {
        transform.Translate(Vector3.back * movementSpeed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.D))
    {
        transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
    }

    //Shooting
    if (Input.GetMouseButtonDown(0))
    {
       Shoot();     
        gunAudio.Play();
    }

}

void Shoot()
{
    Instantiate(bullet, bulletSpawnPoint.transform.position, playerObj.transform.rotation);
}

I was told that using a Vector3 when instantiating my bullet would fix this. But how would I do this? Everything I’ve tried gives me an error or makes it so the bullet doesn’t come out of the tip of the weapon

The simplest way to copy the forward direction of an gameobject is using the transform.forward property.

void Shoot()
{
     var bulletInstance = Instantiate(bullet);
     bullet.transform.position = bulletSpawnPoint.transform.position;
     bullet.transform.forward = player.transform.forward;
}

Im not sure whats causing your errors from the information given, so please provide more information in the future if you want a proper answer (what kind of error, the line in the code etc.)

By the way you dont need to manualy check the direction keys to get a movement direction. A better way would be to use the Input class GetAxis function.

void Move(){
   var horDirection = Input.GetAxis("Horizontal") * Vector3.right;
    var verDirection = Input.GetAxis("Vertical") * Vector3.forward;
    var dir = horDirection + verDirection;
    transform.Translate(dir * Time.deltaTime * movementSpeed);
}