Moving Objects to mouse position!

Looked for an answer online but cant find one. Why is this not working?

working on a 3d game and the Human object just flies away to a random location when i click on it. Iam trying to make it move to mouse position.

Just use Ray. So if you hit something, just MoveTowards it.

First code will smoothly move to the mouse point:


    public float movementSpeed = 10f;

    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100.0f))
            {
                transform.position = Vector3.MoveTowards(transform.position, hit.point, movementSpeed * Time.deltaTime);
            }
        }
    }

Second code will instantly move to the mouse point:


    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100.0f))
            {
                transform.position = hit.point;
            }
        }
    }