Click to Move with Character Controller

Hi Everyone,

I’m trying to move from a rigidbody system to using a character controller because I was having major physics issues with it. I’ve been playing around for days and trying to find an example of click to move with the character controller, but not found anything. What’s happening is when I click somewhere it automatically jumps down the screen and I’m unsure what sort of quaternion rotation or anything I can use with it, all I want to to do is move fluidly like a third party controller over terrain:

public class CharControlAnim : PlayerScript {

    // Speed at which the character moves
	// The Speed the character will move
	
	private Collider col;  
 	public float moveSpeed;
	RaycastHit hit;
	Ray ray;
	public float hitdist = 0.0f;
	public float speed = 3.0F;
    public float rotateSpeed = 3.0F;
	public Transform myTransform;				// this transform
	public Vector3 destinationPosition;		// The destination Point
	public float destinationDistance;      // The distance between myTransform and destinationPosition
    public float smooth = 0.0005F;
 
    void Awake() {
		
	}
 
	void Start () {
	
	var character = GameObject.FindWithTag("Player");	
	myTransform = character.transform;							
		
		
		destinationPosition = myTransform.position;			
	}
 
	void Update () {
 		
		CharacterController controller = GetComponent<CharacterController>();
		// keep track of the distance between this gameObject and destinationPosition
		destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
 
		if(destinationDistance < .5f){		// To prevent shakin behavior when near destination
			moveSpeed = 0;
			Stop();
			animation.CrossFade("idle");
		}
		else if(destinationDistance > .5f){			// To Reset Speed to default
			moveSpeed = 8;
			animation.CrossFade("run");
		}
		
		// Moves the Player if the Left Mouse Button was clicked
		if (Input.GetMouseButtonDown(0)) {
			RaycastHit hit;
			if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
				if(hit.transform){
				destinationPosition = hit.point;
				if(destinationDistance > .5f){
       			 controller.SimpleMove(destinationPosition);
					}
				}
		}
		}
		
	}
}

You can likely get closer to what you want by replacing line 52 - 55 by these lines:

var direction = hit.point - transform.Position;
direction.y = 0.0;
if (direction.magnitude > 0.5) {
    controller.SimpleMove(direction.normalized * speed);
}

Note given how SimpleMove() works, your CC won’t stop automatically when it reaches the destination point. You’ll have to write code that detects the distance and calls…

controller.SimpleMove(Vector3.zero);

…when the distance between the CC and the destination falls below some threshold.

You could use lerp to move slowly from point to click point.