- Home /
Question by
2dkot · Sep 07, 2011 at 07:52 PM ·
charactercontrollercharacter.move
Fail with movedirection of Character Controller
I add character controller to scene, and try to make it to patrol. Moreover character should rotate over terrain. And how can I provide coordinates of Point(to move) as direction. In my case it doesn't work, character moves rapidly somewhere
var i: int;
public var point_arr: Vector3[];
var point_move: Vector3;
var myTarget:GameObject;
var myDistance;
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 10.0;
var normal_q: Quaternion;
var terr_q: Quaternion;
var moveDirection:Vector3;
function Start(){
i = 0;
point_move = point_arr[0];
}
function myMove(mytarget: Vector3){
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
moveDirection = transform.TransformDirection(mytarget);
//moveDirection *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
var hit: RaycastHit;
if(Physics.Raycast (transform.position, -Vector3.up, hit, 10.0))
{
normal_q = Quaternion.FromToRotation(Vector3.up, hit.normal);
terr_q = normal_q * Quaternion.AngleAxis(transform.rotation.eulerAngles.y, hit.normal);
transform.rotation = Quaternion.Slerp(transform.rotation, terr_q, Time.deltaTime);
}
}
function Update() {
point_move = point_arr[i];
point_move.y = transform.position.y;
myDistance = Vector3.Distance(transform.position,point_move);
if(myDistance>1){
myMove(point_arr[i]);
}
else{
if(i==point_arr.length-1){i=0;}
else{i ++;}
}
}
Thanks
Comment
Best Answer
Answer by aldonaletto · Sep 07, 2011 at 08:50 PM
You're calculating moveDirection wrong:
if (controller.isGrounded) { moveDirection = (mytarget-transform.position).normalized; moveDirection.y = 0; // keep only the horizontal direction //moveDirection *= speed; }Maybe you have problems with the rotation to the surface normal, since the CharacterController assume Y is its vertical direction.
Your answer
Follow this Question
Related Questions
charactercontroller.Move Jump not working! 2 Answers
Character controller is shaky/jittery 1 Answer
How to "add force" to a character controller 1 Answer
CharacterController.Move problem with pause menu 2 Answers
hi, how to give controls to a 3d character model. i have downloaded the model as .fbx file 0 Answers