1{
2 CharacterController characterController;
3
4 public float speed = 6.0f;
5 public float jumpSpeed = 8.0f;
6 public float gravity = 20.0f;
7
8 private Vector3 moveDirection = Vector3.zero;
9
10 void Start()
11 {
12 characterController = GetComponent<CharacterController>();
13 }
14
15 void Update()
16 {
17 if (characterController.isGrounded)
18 {
19 // We are grounded, so recalculate
20 // move direction directly from axes
21
22 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
23 moveDirection *= speed;
24
25 if (Input.GetButton("Jump"))
26 {
27 moveDirection.y = jumpSpeed;
28 }
29 }
30
31 // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
32 // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
33 // as an acceleration (ms^-2)
34 moveDirection.y -= gravity * Time.deltaTime;
35
36 // Move the controller
37 characterController.Move(moveDirection * Time.deltaTime);
38 }
39}