Advertisement
salahzar

PlayerController.cs

Feb 7th, 2021
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerController : MonoBehaviour
  4. {
  5.    
  6.     public float speed = 3;
  7.     //public Animator animator;
  8.     // camera and rotation
  9.    
  10.     public float mouseSensitivity = 2f;
  11.     public float upLimit = -50;
  12.     public float downLimit = 50;
  13.  
  14.  
  15.     public Transform cameraHolder;
  16.     public CharacterController characterController;
  17.  
  18.  
  19.     // gravity
  20.     private float gravity = 9.87f;
  21.     private float verticalSpeed = 0;
  22.  
  23.     void Update()
  24.     {
  25.         Move();
  26.         Rotate();
  27.     }
  28.  
  29.     private void Awake()
  30.     {
  31.         Cursor.lockState = CursorLockMode.Locked;
  32.         Cursor.visible = false;
  33.     }
  34.  
  35.     private void Rotate()
  36.     {
  37.         float horizontalRotation = Input.GetAxis("Mouse X");
  38.         float verticalRotation = Input.GetAxis("Mouse Y");
  39.  
  40.         transform.Rotate(0, horizontalRotation * mouseSensitivity, 0);
  41.         cameraHolder.Rotate(-verticalRotation * mouseSensitivity, 0, 0);
  42.  
  43.         Vector3 currentRotation = cameraHolder.localEulerAngles;
  44.         if (currentRotation.x > 180) currentRotation.x -= 360;
  45.         currentRotation.x = Mathf.Clamp(currentRotation.x, upLimit, downLimit);
  46.         cameraHolder.localRotation = Quaternion.Euler(currentRotation);
  47.     }
  48.  
  49.     private void Move()
  50.     {
  51.         float horizontalMove = Input.GetAxis("Horizontal");
  52.         float verticalMove = Input.GetAxis("Vertical");
  53.  
  54.         //if (characterController.isGrounded) verticalSpeed = 0;
  55.         //else verticalSpeed -= gravity * Time.deltaTime;
  56.         //Vector3 gravityMove = new Vector3(0, verticalSpeed, 0);
  57.  
  58.         Vector3 move = transform.forward * verticalMove + transform.right * horizontalMove;
  59.         characterController.Move(speed * Time.deltaTime * move /*  + gravityMove * Time.deltaTime */);
  60.  
  61.         //animator.SetBool("isWalking", verticalMove != 0 || horizontalMove != 0);
  62.  
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement