Need in 2d game, Player shoot enemy

I am working on a 2d Game. player shoot at mouse click

My code doesn’t work accurately

Weapon

using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour
{
#region Varabiles
[SerializeField]
private Transform firePoint, bulletTrail;

[SerializeField] float waitForNextFire, fireCounter;

[SerializeField] bool canFire;

[SerializeField]
private LayerMask hitLayerMask;

Vector2 mousePos, firePointPosition;

public int bulletCount;
private int maxBullets;
#endregion

#region Unity Methods
void Awake () 
{
    firePoint = transform.Find("Fire Point");
	if(firePoint == null)
		Debug.LogError("fire point missing");

    fireCounter = waitForNextFire;

    maxBullets = 10;
    bulletCount = maxBullets;
}

void Update () 
{       

    fireCounter -= Time.deltaTime;

    if(Input.GetMouseButtonDown(0))
    {
        
        bulletCount--;

        if (bulletCount >= 0)
            Shoot();

        fireCounter = waitForNextFire;
    }
}
#endregion

#region Methods
void Shoot()
{
    mousePos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);

    RaycastHit2D hit = Physics2D.Raycast(firePointPosition, mousePos - firePointPosition, 50f, hitLayerMask);

    Instantiate(bulletTrail, firePoint.position, transform.localRotation);
}
#endregion

Bullet
using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour
{
private int moveSpeed = 25;
private Vector3 direction;

private void Awake()
{
    direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    Debug.Log("bullet rotation" + transform.localRotation);
}
void Update () 
{
    // transform.Translate(direction * Time.deltaTime * moveSpeed);
    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    Destroy(gameObject, 2f);
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.tag == "Enemy")
    {
        Destroy(this.gameObject);
    }
}

}
HELP PLEASE

You would need to be more specific. What doesn’t work? The firing? The bullet being fired? The direction of fire? The enemy being hit?

EDIT: So it seems like your problem is the direction. Here are some things that you might want to check:

  1. In your Bullet class, you’re using Translate without the extra parameter, so it defaults to Space.Self. You might want to change it to: Space.World.

  2. You’re using Vector3.forward as your forward direction for the bullet. That will propel your bullet towards the horizon (z) which you won’t want since this is a 2d game. Instead, use transform.right.

Here’s how the updated code for Bullet will look like:

void Awake()
{
    direction = transform.right;
}

void Update () {
    transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
}