Make player follow mouse position

I have a space ship in a 2D top down game. Ive been messing around with a script from the unity answers. Here is the script:

using UnityEngine;
using System.Collections;

public class playerController : MonoBehaviour
{
public float speed = 1.5f;
private Vector3 target;
public Camera PlayerCamera;

void Start()
{
    target = transform.position;
}

void Update()
{
    if (Input.GetKey(KeyCode.Mouse0))
    {
        target = PlayerCamera.ScreenToWorldPoint(Input.mousePosition);
        target.y = transform.position.y;
        target = new Vector3(target.y, transform.position.x, target.z);
        transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    }
    //transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}

}

when I click the mouse0 button my player moves a little to the left and stops. I do not know why this is.
At the moment I am just trying to get it to follow the mouse but eventually the player will rotate to go a different direction when the mouse position is changed.

Thank you for your help!

Here’s a simple approach that can also move smoothly:

using System;
using UnityEngine;

public class NewBehaviourScript1 : MonoBehaviour
{
    private Vector3 _target;
    public Camera Camera;
    public bool FollowMouse;
    public bool ShipAccelerates;
    public float ShipSpeed = 2.0f;

    public void OnEnable()
    {
        if (Camera == null)
        {
            throw new InvalidOperationException("Camera not set");
        }
    }

    public void Update()
    {
        if (FollowMouse || Input.GetMouseButton(0))
        {
            _target = Camera.ScreenToWorldPoint(Input.mousePosition);
            _target.z = 0;
        }

        var delta = ShipSpeed*Time.deltaTime;
        if (ShipAccelerates)
        {
            delta *= Vector3.Distance(transform.position, _target);
        }

        transform.position = Vector3.MoveTowards(transform.position, _target, delta);
    }
}

(I did the following setup: added a sprite object and that script to it, then modified main camera to an orthographic projection.)

Try changing Update to FixedUpdate.

So, you just want the object to follow the mouse? If that’s the case, I’m pretty sure that you only need two of the four lines of movement code that you have.

Try using just these two lines:

     target = PlayerCamera.ScreenToWorldPoint(Input.mousePosition);
     transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);

Or, if you wanted, you could even simplify it further and just go:

     transform.position = Vector3.MoveTowards(transform.position, PlayerCamera.ScreenToWorldPoint(Input.mousePosition), speed * Time.deltaTime);

Hope this helps!