Skip to content

Instantly share code, notes, and snippets.

@calderonsteven
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save calderonsteven/e5c9dffa39a8a4fad2dd to your computer and use it in GitHub Desktop.

Select an option

Save calderonsteven/e5c9dffa39a8a4fad2dd to your computer and use it in GitHub Desktop.
Move the controller using CharacterController.move function.
#pragma strict
var walkSpeed = 150;
var rotateSpeed = 100;
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(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= walkSpeed * Time.deltaTime;
//also with jump ;)
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
// rotate controller
var horizontalDir = parseFloat(Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime);
transform.Rotate(0, horizontalDir, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment