Advertisement
Exlim

Untitled

Sep 2nd, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace ExlimDev
  6. {
  7.     public class PlayerLocomotion : MonoBehaviour
  8.     {
  9.         Transform cameraObject;
  10.         InputHandler inputHandler;
  11.         Vector3 moveDirection;
  12.  
  13.         [HideInInspector]
  14.         public Transform myTransform;
  15.         [HideInInspector]
  16.         public AnimatorHandler animatorHandler;
  17.  
  18.         public new Rigidbody rigidbody;
  19.         public GameObject normalCamera;
  20.  
  21.         [Header("Stats")]
  22.         [SerializeField]
  23.         float movementSpeed = 5;
  24.         [SerializeField]
  25.         float rotationSpeed = 10;
  26.  
  27.         void Start()
  28.         {
  29.             rigidbody = GetComponent<Rigidbody>();
  30.             inputHandler = GetComponent<InputHandler>();
  31.             animatorHandler = GetComponentInChildren<AnimatorHandler>();
  32.             cameraObject = Camera.main.transform;
  33.             myTransform = transform;
  34.             animatorHandler.Initialize();
  35.         }
  36.  
  37.         public void Update()
  38.         {
  39.             float delta = Time.deltaTime;
  40.  
  41.             inputHandler.TickInput(delta);
  42.  
  43.             moveDirection = cameraObject.forward * inputHandler.vertical;
  44.             moveDirection += cameraObject.right * inputHandler.horizontal;
  45.             moveDirection.Normalize();
  46.  
  47.             float speed = movementSpeed;
  48.             moveDirection *= speed;
  49.  
  50.             Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
  51.             rigidbody.velocity = projectedVelocity;
  52.  
  53.             if (animatorHandler.canRotate)
  54.             {
  55.                 HandleRotation(delta);
  56.             }
  57.         }
  58.  
  59.         #region Movement
  60.         Vector3 normalVector;
  61.         Vector3 targetPosition;
  62.  
  63.         private void HandleRotation(float delta)
  64.         {
  65.             Vector3 targetDir = Vector3.zero;
  66.             float moveOverride = inputHandler.moveAmount;
  67.  
  68.             targetDir = cameraObject.forward * inputHandler.vertical;
  69.             targetDir += cameraObject.right * inputHandler.horizontal;
  70.  
  71.             targetDir.Normalize();
  72.             targetDir.y = 0;
  73.  
  74.             if (targetDir == Vector3.zero)
  75.                 targetDir = myTransform.forward;
  76.  
  77.             float rs = rotationSpeed;
  78.  
  79.             Quaternion tr = Quaternion.LookRotation(targetDir);
  80.             Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
  81.  
  82.             myTransform.rotation = targetRotation;
  83.         }
  84.  
  85.         #endregion
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement