Advertisement
Guest User

Movement

a guest
Mar 28th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. [RequireComponent(typeof(CharacterController))]
  2. public class MovementHero : MonoBehaviour
  3. {
  4.     [Header("Скорость персонажа")]
  5.     [Range(0.5f, 35)]
  6.     public float SpeedMovement = 1f;
  7.  
  8.     [Header("Чувствительность мыши")]
  9.     [Range(0.1f, 1f)]
  10.     public float Sensivity = 1f;
  11.  
  12.     [Header("Минимальный угол просмотра")]
  13.     [Range(0f, -120f)]
  14.     public float YMinLimit = -45f;
  15.  
  16.     [Header("Максимальный угол просмотра")]
  17.     [Range(120f, 0f)]
  18.     public float YMaxLimit = 45f;
  19.  
  20.     [Header("Сила гравитации")]
  21.     public float GlobalGravity = 9.81f;
  22.  
  23.     [Header("Максимальная дистанция от земли")]
  24.     public float MaxDistance = 0f;
  25.  
  26.     CharacterController movementController;
  27.     Transform mainCamera;
  28.  
  29.     float xRotate, yRotate = 0f;
  30.  
  31.     private void Start()
  32.     {
  33.         movementController = gameObject.GetComponent<CharacterController>();
  34.         mainCamera = Camera.main.transform;
  35.  
  36.         // Lock the cursor
  37.         Cursor.lockState = CursorLockMode.Locked;
  38.         Cursor.visible = false;
  39.     }
  40.  
  41.     private void FixedUpdate()
  42.     {
  43.         float horizontal = Input.GetAxis("Horizontal");
  44.         float vertical = Input.GetAxis("Vertical");
  45.  
  46.         Vector3 direction = new Vector3(horizontal, 0, vertical);
  47.  
  48.         movementController.Move(direction * SpeedMovement * Time.fixedDeltaTime);
  49.     }
  50.  
  51.     private void Update()
  52.     {
  53.         Vector3 mousePos = Input.mousePosition;
  54.         float kefAngel = ((Screen.width - mousePos.x) / Screen.width);
  55.  
  56.         kefAngel = kefAngel < 0 ? kefAngel * 2 : kefAngel;
  57.         kefAngel = Mathf.Clamp(kefAngel, -1, 1); // Normalize
  58.  
  59.         xRotate = mousePos.y * Sensivity * 0.15f;
  60.         xRotate = Mathf.Clamp(xRotate, YMinLimit, YMaxLimit);
  61.  
  62.         yRotate = 360 * kefAngel * Sensivity;
  63.  
  64.         transform.rotation = Quaternion.Euler(0, -yRotate, 0);
  65.         mainCamera.rotation = Quaternion.Euler(-xRotate, yRotate, 0);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement