eastbayeff

Untitled

Oct 18th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(CharacterController))]
  6.  
  7. public class SC_FPSController : MonoBehaviour
  8. {
  9.     public float walkingSpeed = 7.5f;
  10.     public float runningSpeed = 11.5f;
  11.     public float jumpSpeed = 8.0f;
  12.     public float gravity = 20.0f;
  13.     public Camera playerCamera;
  14.     public float lookSpeed = 2.0f;
  15.     public float lookXLimit = 45.0f;
  16.  
  17.     CharacterController characterController;
  18.     Vector3 moveDirection = Vector3.zero;
  19.     float rotationX = 0;
  20.  
  21.     [HideInInspector]
  22.     public bool canMove = true;
  23.  
  24.     void Start()
  25.     {
  26.         characterController = GetComponent<CharacterController>();
  27.  
  28.         // Lock cursor
  29.         Cursor.lockState = CursorLockMode.Locked;
  30.         Cursor.visible = false;
  31.     }
  32.  
  33.     void Update()
  34.     {
  35.         // We are grounded, so recalculate move direction based on axes
  36.         Vector3 forward = transform.TransformDirection(Vector3.forward);
  37.         Vector3 right = transform.TransformDirection(Vector3.right);
  38.         // Press Left Shift to run
  39.         bool isRunning = Input.GetKey(KeyCode.LeftShift);
  40.         float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
  41.         float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
  42.         float movementDirectionY = moveDirection.y;
  43.         moveDirection = (forward * curSpeedX) + (right * curSpeedY);
  44.  
  45.         if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
  46.         {
  47.             moveDirection.y = jumpSpeed;
  48.         }
  49.         else
  50.         {
  51.             moveDirection.y = movementDirectionY;
  52.         }
  53.  
  54.         // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
  55.         // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
  56.         // as an acceleration (ms^-2)
  57.         if (!characterController.isGrounded)
  58.         {
  59.             moveDirection.y -= gravity * Time.deltaTime;
  60.         }
  61.  
  62.         // Move the controller
  63.         characterController.Move(moveDirection * Time.deltaTime);
  64.  
  65.         // Player and Camera rotation
  66.         if (canMove)
  67.         {
  68.             rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
  69.             rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
  70.             playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
  71.             transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment