- Home /
Click to Move with CharacterController
I am using this script:
var smooth:int;
private var targetPosition:Vector3;
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
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;
}
}
transform.position = Vector3.MoveTowards (transform.position, targetPosition, Time.deltaTime * smooth);
//var controller : CharacterController = GetComponent(CharacterController);
//controller.Move(Vector3(0, 0, smooth * Time.deltaTime));
}
But I can't figure out how to do move with CharacterController, so collisions are detected.
Answer by hexagonius · Mar 29, 2013 at 08:35 PM
There's a few things to know about CharacterControllers:
Their skinwidth is like a forcefield. It keeps the Controller from running through objects all by itself. Since a minimum speed is required to penetrate it you better not count on getting any OnCollisionEnter/Stay/Exit calls. OnTriggerEnter/Stay/Exit will still be called.
There is the Method OnControllerColliderHit which is called on the CharacterController exclusively, so no change using that directly on an object. When colliding with another CharacterController nothing will be called.
Using SimpleMove on it will ignore all y-Axis changes and implement gravity by itself according to the global gravity value.
Using Move leaves you with full control over all 3 Axis but does not support gravity. You will have to implement it yourself (like setting y to -gravity when grounded)
Thank you, I used simple$$anonymous$$ove and it worked.