charactercontroller.move() dont work right

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.

have you verified that moveDirection.y is not zero? in other words- is your jump button event even being called?

–

yes 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

–

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 :stuck_out_tongue:

The character dont move forward, but the other directions work. i have no joystick, script is applied, taged it player(did nothing) I really have no idea here..

–

Yes, I know for sure it's not null. That's why I'm confused. I got it working with: targetableObjects.Remove(destroyedObject); players.Remove(destroyedObject); but this is only a temporary solution.

–

This is solved, this code is corrent!
Problem was i parented in stuff with the character that had colliders ._.