var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
This code dont work as i should for me, the positive vertical value dont work right.
Anyone had the same problem?
This code is direct from the unity script reference manual.
2 Answers
2
var speed : float = 7.0;
var jumpSpeed : float = 4.0;
var gravity : float = 5.0;
var rotateSpeed : float = 3.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
if (controller.isGrounded) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
@script RequireComponent(CharacterController)
The character doesn’t move or the character moves in strange ways?
Make sure that you don’t have any joystick turned on the computer!
Make sure that you applied the script on the character! XD (Sometimes happens to me)
And tag the character as a player too 
This is solved, this code is corrent!
Problem was i parented in stuff with the character that had colliders ._.
have you verified that moveDirection.y is not zero? in other words- is your jump button event even being called?
– testureyes the values is all correct but the controller.move some how mess them up. values are -2.0 - 2.0 for x,y and z when buttons are pressed
– anon97462488