- Home /
Charactercontroller.move affecting rotation
so charactercontroller.simplemove moves the character in relation to it's rotation. for example, I use simplemove to up the x axis. I rotate the character 90 degrees, and now it moves up the z axis. This is what I want.
However, I also want the character to jump; charactercontroller.move is suggested for that. it moves the character globally though, it always moves along the same axis no matter the rotation. How can I fix that?
Answer by aldonaletto · Jun 19, 2013 at 11:50 PM
SimpleMove and Move both expect a global direction, not local. The difference between them is that SimpleMove expects the velocity vector, while Move wants the displacement. I suspect that you're using the example scripts - they are different: the SimpleMove example rotates with the horizontal arrows, while the Move example rotates with the mouse and moves laterally with left/right keys. If this is the case, you must create a "Frankenstein" script with parts of both - like this:
var speed : float = 6.0;
var rotSpeed : float = 90.0; // rotate at 90 degrees/second
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
// rotate character with horizontal keys:
transform.Rotate(0, Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime, 0);
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3.forward * Input.GetAxis("Vertical");
// convert direction from local to global space:
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// convert velocity to displacement and move character:
controller.Move(moveDirection * Time.deltaTime);
}
O$$anonymous$$G This solved the problem I had from months!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! LOVE YOU!
Not exactly what I was looking for but with some tweaks, it worked