Why does this script not work?

I’m trying to make my character stay on the ground no matter what. There is no gravity in the game, thats why Im trying to do it like this, like runescape I guess.

I honestly dont know what I did wrong, if anyone can fix it or tell me what to do, to make something stay on the ground, it would help a lot. Thanks in advance.

private var targetPosition:Vector3; 
var speed : float = 60;      
var clickEffect : GameObject;
 
 
        function Update () {
   var hit : RaycastHit;
    if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
        var distanceToGround = hit.distance;
          transform.position.y = hit.distance - transform.collider.bounds.extents;
    }
            if(Input.GetKeyDown(KeyCode.Mouse0))
 
            {
 
 
           speed = 3;
 
                var playerPlane = new Plane(Vector3.up, transform.position);
 
                var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
                var hitdist = 0.0;
 
 
 
                if (playerPlane.Raycast (ray, hitdist)) {
 
                    var targetPoint = ray.GetPoint(hitdist);
 
                    targetPosition = ray.GetPoint(hitdist);
 
                    var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
 
                    transform.rotation = targetRotation;
 
 
 
                }
 
 
 
            }
 
 
 
                var dir:Vector3 = targetPosition - transform.position;
 
    var dist:float = dir.magnitude;
 
    var move:float = speed * Time.deltaTime;
 
    if(dist > move){
 
    transform.position += dir.normalized * move;
    Instantiate(clickEffect,targetPosition,Quaternion.identity);
 
    } else {
 
    transform.position = targetPosition;
 
    }
 
 
 
 
 
            transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
 
 
 
 
 
 
 
 
 
         }

Regardless of your script’s function, you can choose Freeze Position > Y under Constraints on your player’s Rigidbody component.