Click to Move - Character Controller Problem

Hey Guys,

I’m currently developing a game where you click and the player moves to that location. I’ve previously used Rigidbody but wasn’t pleased with the outcome, so after researching I found out Character Controller is better for Third Person Games. Is there any other ways of moving the character with mouse click, but collision has to be detected as it’ll have a major role in the game.

Note: I’ve played around a lot with the script but the results weren’t great, I’ve also researched the forums, answers section for a solution.

This is the script I have so far, any help would be appreciated.

var MovementSpeed : float = 6.0;
var JumpSpeed : float = 8.0;
var Gravity : float = 20.0;
var RaycastDistance : float = 100;
@HideInInspector
var MovementEnabled : boolean;
var MoveDirection : Vector3;

function Update() 
{
    
//Create Character Controller
    var PlayerController : CharacterController = GetComponent(CharacterController);
    
    //Create Raycast
    var NewRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    
    //Store Location of RaycastHit Position
    var RayHit : RaycastHit;
    
    //Check if Character is Grounded
    if (PlayerController.isGrounded) 
    {
    	if(Input.GetButtonDown("Fire1")) 
    	{    		
    		//Detect if any collider is hit within the RaycastDistance
	    	if (Physics.Raycast(NewRay, RayHit, RaycastDistance))
	    	{	 	
	            //Get RayHit Point
	            var MovePoint = RayHit.point;
	            Debug.Log(MovePoint);   	
	    	}
    	}
    	
        if (Input.GetButton ("Jump")) 
        {
            MoveDirection.y = JumpSpeed;
        }
        
    }
  
    //Apply gravity
    MoveDirection.y -= Gravity * Time.deltaTime;	        
    
    //Move the controller
	PlayerController.Move(MoveDirection * Time.deltaTime);
								
}

Screen point to ray works best, but you can use a navmeshagent which will avoid static obstacles in the free version if you bake a navmesh. Works great in combination with Mecanim, I think. I only use two animations, and it looks pretty decent on turns, startup, slowdown, etc. Of course, you have to code in the Mecanim changes from the velocity of the agent. I ended up using absolute values so he didn’t go back into idle on turns.