Advertisement
GhostSPJ

Test5

Aug 1st, 2021
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5.  
  6. public class AnimationAndMovementController : MonoBehaviour
  7. {
  8.  
  9.     PlayerInput playerInput;
  10.     CharacterController characterController;
  11.  
  12.  
  13.     Animator animator;
  14.  
  15.     int isWalkingHash;
  16.     int isRunningHash;
  17.  
  18.     Camera _camera;
  19.  
  20.     Vector2 currentMovementInput;
  21.     Vector3 currentMovement;
  22.     Vector3 currentRunMovement;
  23.  
  24.     bool isMovementPressed;
  25.     bool isRunPressed;
  26.  
  27.     float rotationFactorPerFrame = 15.0f;
  28.     float runMultiplier = 3.0f;
  29.  
  30.  
  31.  
  32.     // Start is called before the first frame update
  33.     void Start()
  34.     {
  35.         _camera = Camera.main;
  36.     }
  37.  
  38.     void Awake()
  39.     {
  40.         playerInput = new PlayerInput();
  41.         characterController = GetComponent<CharacterController>();
  42.         animator = GetComponent<Animator>();
  43.  
  44.         isWalkingHash = Animator.StringToHash("isWalking");
  45.         isRunningHash = Animator.StringToHash("isRunning");
  46.  
  47.  
  48.         playerInput.CharacterControls.Move.started += onMovementInput;
  49.         playerInput.CharacterControls.Move.canceled += onMovementInput;
  50.         playerInput.CharacterControls.Move.performed += onMovementInput;
  51.  
  52.         playerInput.CharacterControls.Run.started += onRun;
  53.         playerInput.CharacterControls.Run.canceled += onRun;
  54.  
  55.     }
  56.  
  57.  
  58.     void onRun(InputAction.CallbackContext context)
  59.     {
  60.         isRunPressed = context.ReadValueAsButton();
  61.     }
  62.  
  63.  
  64.     void onMovementInput(InputAction.CallbackContext context)
  65.     {
  66.  
  67.         currentMovementInput = context.ReadValue<Vector2>();
  68.         currentMovement.x = currentMovementInput.x;
  69.         currentMovement.z = currentMovementInput.y;
  70.  
  71.         currentRunMovement.x = currentMovementInput.x * runMultiplier;
  72.         currentRunMovement.z = currentMovementInput.y * runMultiplier;
  73.  
  74.         isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
  75.  
  76.     }
  77.  
  78.  
  79.  
  80.     void handleAnimation()
  81.     {
  82.         bool isWalking = animator.GetBool(isWalkingHash);
  83.         bool isRunning = animator.GetBool(isRunningHash);
  84.  
  85.         if (isMovementPressed && !isWalking)
  86.         {
  87.             animator.SetBool(isWalkingHash, true);
  88.         }
  89.         else if (!isMovementPressed && isWalking)
  90.         {
  91.             animator.SetBool(isWalkingHash, false);
  92.         }
  93.  
  94.         if ((isMovementPressed && isRunPressed) && !isRunning)
  95.         {
  96.             animator.SetBool(isRunningHash, true);
  97.         }
  98.         else if ((!isMovementPressed && !isRunPressed) && isRunning)
  99.         {
  100.             animator.SetBool(isRunningHash, false);
  101.         }
  102.         else if ((isMovementPressed && !isRunPressed) && isRunning)
  103.         {
  104.             animator.SetBool(isRunningHash, false);
  105.         }
  106.  
  107.     }
  108.  
  109.  
  110.     void handleGravity()
  111.     {
  112.         if (characterController.isGrounded)
  113.         {
  114.             float groundedGravity = -0.05f;
  115.             currentMovement.y = groundedGravity;
  116.             currentRunMovement.y = groundedGravity;
  117.         }
  118.         else
  119.         {
  120.             float gravity = -9.8f;
  121.             currentMovement.y = gravity;
  122.             currentRunMovement.y = gravity;
  123.         }
  124.     }
  125.  
  126.     void handle_isRunPressed()
  127.     {
  128.         if (isRunPressed)
  129.         {
  130.             characterController.Move(currentRunMovement * Time.deltaTime);
  131.         }
  132.         else
  133.         {
  134.             characterController.Move(currentMovement * Time.deltaTime);
  135.         }
  136.     }
  137.  
  138.     void handleRotation()
  139.     {
  140.         // We're getting a Vector2, whereas we will need a Vector3
  141.         // Get a z value based on camera, and include it in a Vector3
  142.         Vector2 mousePosition = playerInput.CharacterControls.MousePosition.ReadValue<Vector2>();
  143.  
  144.         var mousePositionZ = _camera.farClipPlane * .5f;
  145.  
  146.         Vector3 mouseViewportPosition = _camera.ViewportToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, _camera.transform.position.y));
  147.  
  148.  
  149.         Debug.Log("MousePos: " + mouseViewportPosition);
  150.  
  151.         Vector3 positionToLookAt;
  152.         positionToLookAt.x = mouseViewportPosition.x;
  153.         positionToLookAt.y = 0.0f;
  154.         //positionToLookAt.z = currentMovement.z;
  155.         positionToLookAt.z = mouseViewportPosition.z;
  156.  
  157.  
  158.        
  159.  
  160.  
  161.         Quaternion currentRotation = transform.rotation;
  162.        
  163.         Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt - transform.position);
  164.         transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
  165.        
  166.     }
  167.  
  168.  
  169.     float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
  170.     {
  171.         return Mathf.Atan2(b.y - a.y, b.x - a.x) * Mathf.Rad2Deg;
  172.     }
  173.  
  174.  
  175.  
  176.     // Update is called once per frame
  177.     void Update()
  178.     {
  179.         handleRotation();
  180.         handleAnimation();
  181.         handleGravity();
  182.  
  183.         handle_isRunPressed();
  184.     }
  185.  
  186.  
  187.     void OnEnable()
  188.     {
  189.         playerInput.CharacterControls.Enable();
  190.     }
  191.  
  192.     void OnDisable()
  193.     {
  194.         playerInput.CharacterControls.Disable();
  195.     }
  196.  
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement