Last active
August 29, 2015 14:22
-
-
Save calderonsteven/e5c9dffa39a8a4fad2dd to your computer and use it in GitHub Desktop.
Move the controller using CharacterController.move function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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