Make character follow mouse

Hi! I am making a space exploration game with upside down camera view, and i wanted help in making the character follow the mouse.

I don’t know what to use because i never used mouse for anything unless to just be a trigger to something, so i don’t know exactly how to do this. I wanted the player to have a variable or public float so i could change the speed that it goes toward the mouse, and i wanted to make the player automatically and almost instantly rotate where the mouse is pointing and then follow it if the player is pressing Fire1. Something like the game Asteroids

Can someone help me? I really don’t know how to do this… The nearest thing i’ve done like this was a script that allowed an object to follow the player, but i don’t know what to do about the mouse. I would really appreciate if anyone could help me in this one, because i wanted to release a free space exploration game, and this is one of the few problems that i am having.

The most simple way to do it, it’s to create a Plane that is the same height of your character them you use Plane.Raycast and use ScreenPointToRay when the player pressed the mouse button for to get the target position them move the character (there are a lot of ways to do it).

But I wouldn’t do like that, do some research about NavMeshlink text.
Here is a Unity tutorial learning how to move a character with mouse and NavMesh: Unity - Manual: Moving an Agent to a Position Clicked by the Mouse

Sorry for my bad English

public class MouseControls : MonoBehaviour {

    public float speed = 5.0f;

    public void Update() {
        Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit) {
            this.transform.LookAt(hit.point);
            if(Input.GetButton("Fire 1") && Vector3.Distance(this.transform.position, hit.point) > 0,0f) {
                this.transform.Translate(Vector3.forward * Time.deltaTime * this.speed);
            }
        }
    }

}

shoud do it.

This script should work if your game is literally in top down camera view, if not then you’ll just need to lock the rotation and movement accordingly.
This script doesn’t use raycasting which is better for performance (I think) and instead of making the player move forward, he moves towards the mouse which eliminates the possibility of him going in random directions when inside of mouse position.

    public float speed;
    Vector3 mousePos;

	void Start ()
    {
	    
	}
	
	void Update ()
    {
        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.LookAt (mousePos);
        transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, transform.localEulerAngles.z);

        if (Input.GetMouseButton(0))
            transform.position += (-transform.position + mousePos).normalized * speed * Time.deltaTime;
        transform.position = new Vector3(transform.position.x, 0, transform.position.z);
    }