- Home /
Character Controller won't move forward
My character only moves forward when I jump. I don't know what to do. Please help.
float speed = 6.0;
float jumpSpeed = 8.0;
float gravity = 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 is a pretty standard script. Have you changed any settings for the "Horizontal" or "Vertical" axes? For example, if you've changed them to you a joystick but you don't have a joystick, they won't work. Also check the value of 'speed' in the inspector. The value assigned to 'speed' in code will only be used at the time the script attached.
I checked the settings and there doesn't seem to be anything wrong about it. The speed value in the inspector is correct.
Insert a Debug.Log() at line 9:
Debug.Log(Input.GetAxis("Horizontal") + ", " + Input.GetAxis("Vertical");
Note this also might be a collision issue. If your character has child objects with colliders, your CharacterController will interpret them as obstacles. If you have child objects, turn off their colliders to start.
Input.GetAxis seems to be working just fine. I don't see any child objects with colliders in my character. I can move my character from left to right just fine, But it just won't move forward.