PlayerController.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent( typeof (CharacterController))] public class PlayerController : MonoBehaviour { public float speed = 3; public float rotationSpeed = 90; public float gravity = -20f; public float jumpSpeed = 15; CharacterController characterController; Vector3 moveVelocity; Vector3 turnVelocity; void Awake() { characterController = GetComponent<CharacterController>(); } void Update() { var hInput = Input.GetAxis( "Horizontal" ); var vInput = Input.GetAxis( "Vertical" ); if (characterController.isGrounded) { moveVelocity = transform.forward * speed * vInput; turnVelocity = transform.up * rotationSpeed * hInput; if (Input.GetButtonDown( "Jump" )) { moveVelocity.y = jumpSpeed; } } //Adding gravity moveVelocity.y += gravity * Time.deltaTime; characterController.Move(moveVelocity * Time.deltaTime); transform.Rotate(turnVelocity * Time.deltaTime); } } |