Shoot bullet to the direction of the player

First of all, sorry for posting this because I know that it has been asked several times before, but none of the answers seem to be working. Being a beginner in making guns, I am asking for help.

Here is the player code

public class PlayerMovement : MonoBehaviour
{
    public float speed;

    private Vector3 mouse_pos;
    public Transform target;
    private Vector3 object_pos;
    private float angle;

    public GameObject bullet;
    public Transform     firePoint;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {

        mouse_pos = Input.mousePosition;
        mouse_pos.z = -10;
        object_pos = Camera.main.WorldToScreenPoint(target.position);
        mouse_pos.x = mouse_pos.x - object_pos.x;
        mouse_pos.y = mouse_pos.y - object_pos.y;
        angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0, 0, angle);


        if (Input.GetKey(KeyCode.W))
        {
            transform.position += Vector3.up * speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.S))
        {
            transform.position += Vector3.down * speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.D))
        {
            transform.position += Vector3.right * speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.A))
        {
            transform.position += Vector3.left * speed * Time.deltaTime;
        }

        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(bullet, firePoint.position, gameObject.transform.rotation);
        }
    }
}

And here is the bullet code

public class Bullet : MonoBehaviour
{
    public float speed;
    public Transform player;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector2(transform.position.x + 0.25f * speed, transform.position.y);
    }
}

If you got any suggestions, please tell me. I really want to get this working to improve my skills.

Thanks in advance!

Direction is obtained by subtracting one vector from the other:

Vector3 PlayerPosition = ...
Vector3 TargetPosition = ...

Vector3 Direction = TargetPosition - PlayerPosition;

// now you can normalize Direction and multiply it by speed to use it

Other than that, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Thanks, but I still don’t undesrtand it all. If you (or someone else) give me some more details, it would be really appreciated.

Thanks a lot

Probably best to pick a few random Youtube tutorials involving guns and aiming and work through them, paying particular attention how stuff is aimed.

Ok thanks! Sorry for taking your time

I got it working! Thanks!

1 Like

I believed in you! Go forth and be excellent always.