Character falls through the ground

Here is my movement script.

When I move the player he falls through the ground.

Please help.

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour {
	
	public RaycastHit hit;
	public Ray ray;
	public Vector3 direction;
	public float moveSpeed = 3f;
	public int rotateSpeed = 75;
	private Transform PlayerTransform;
	
	
	
	// Use this for initialization
	void Start () 
	{
		PlayerTransform = transform;
		direction = PlayerTransform.position;
	}
	
	// Update is called once per frame
	void FixedUpdate () 
		
	{  
		if(Input.GetMouseButton(0))
			
		{
			ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if(Physics.Raycast (ray, out hit, 100))
			{         
				direction = hit.point;
				
				PlayerTransform.position = Vector3.MoveTowards(PlayerTransform.position, hit.point, moveSpeed * Time.deltaTime);
				Debug.DrawLine(PlayerTransform.position, hit.point, Color.blue, 2);
				
				Vector3 targetPoint = hit.point;

				PlayerTransform.position = Vector3.MoveTowards(PlayerTransform.position, hit.point, moveSpeed * Time.deltaTime);
				targetPoint.y = 0;
			}
			
			
		}
		
	}
}

I am not sure why you’re altering the position twice when the ray hits, I guess that’s just because you’re in the middle of trying things? Also, directly changing the position of a physics object (i.e. rigidbody) is asking for trouble, you’d probably be better of using the rigidbody.MovePosition method in this case.

But both those things are probably not why your character is falling trough the floor. The problem (probably) is that the hit.point gives you the point where the ray hits which is likely to be on the floor. If you move your character to that point, you’re moving its origin to that point. Chances are that the collider of the character extend below that point, and by placing the origin of the character on the hit.point you’re placing it in the floor which makes the collision ignore the floor and thus make it fall trough.

To solve this you could either move the collider (and the mesh etc.) up by giving it an offset, or add an offset to the hit point:

public Vector3 offset = new Vector3(0, 2, 0);

void FixedUpdate()
{
  ...

  Vector3 targetPoint = hit.point + offset;
  
  ...
}