└── PlayerController.cs /PlayerController.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | // Ensures the required component is attached to the current object 4 | [RequireComponent(typeof(CharacterController))] 5 | public class PlayerController : MonoBehaviour 6 | { 7 | // This makes use of the character controller class for fast QUAKE/DOOM-like movement 8 | CharacterController characterController; // Character controller variable to store reference to attached script 9 | Vector3 movementDirection = Vector3.zero; // Vector3 variable to store current movement direction 10 | 11 | [SerializeField] protected GameObject characterHead; // Player head which is a child object of the current object this script is attached to 12 | 13 | [SerializeField] protected float lookSpeed = 30.0f; 14 | [SerializeField] protected float movementSpeed = 10.0f; 15 | [SerializeField] protected float jumpForce = 10.0f; 16 | [SerializeField] protected float gravity = 9.807f; // We have to manually define and apply gravity due to the use of the character controller unity class for movement 17 | 18 | [SerializeField] protected bool invertVerticalLookDirection; // Specify whether look should be inverted, this can be done in a nicer way if your familiar with unity's input system 19 | 20 | protected virtual void Start() 21 | { 22 | // Setup attached character controller 23 | characterController = GetComponent(); 24 | characterController.enableOverlapRecovery = true; 25 | } 26 | 27 | protected virtual void Update() 28 | { 29 | // Handle input 30 | float lookRight = Input.GetAxisRaw("Mouse X"); 31 | float lookUp = Input.GetAxisRaw("Mouse Y"); 32 | 33 | float moveRight = Input.GetAxisRaw("Horizontal"); 34 | float moveForward = Input.GetAxisRaw("Vertical"); 35 | 36 | bool isJumping = Input.GetButtonDown("Jump"); 37 | 38 | // Call functions using input 39 | Move(moveRight, moveForward, isJumping); 40 | Look(lookRight, lookUp); 41 | 42 | movementDirection.y -= gravity * Time.deltaTime; // Apply gravity over time 43 | characterController.Move(movementDirection * Time.deltaTime); // Move using the CharacterController class 44 | } 45 | 46 | public void Move(float moveRight, float moveForward, bool isJumping) 47 | { 48 | if (characterController.isGrounded) // Check if toouching floor 49 | { 50 | movementDirection = new Vector3(moveRight, 0.0f, moveForward); // Apply movement from input 51 | movementDirection = transform.TransformDirection(movementDirection); // Make movement relative to rotation 52 | movementDirection *= movementSpeed; // Move at specified speed 53 | 54 | if (isJumping) { movementDirection.y = jumpForce; } // Apply jump force if input 55 | } 56 | } 57 | 58 | public void Look(float lookRight, float lookUp) 59 | { 60 | float verticalLookDelta = (lookUp * lookSpeed) * Time.deltaTime; 61 | float horizontalLookDelta = (lookRight * lookSpeed) * Time.deltaTime; 62 | 63 | float desiredVerticalLookDelta = (invertVerticalLookDirection) ? verticalLookDelta : -verticalLookDelta; // Inver look direction if specified 64 | 65 | transform.Rotate(new Vector3(0.0f, horizontalLookDelta, 0.0f)); // Rotate object around Y axis 66 | characterHead.transform.Rotate(new Vector3(desiredVerticalLookDelta, 0.0f, 0.0f)); // Rotate Head around X axis 67 | } 68 | } --------------------------------------------------------------------------------