Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class AnimationAndMovementController : MonoBehaviour
- {
- PlayerInput playerInput;
- CharacterController characterController;
- Animator animator;
- int isWalkingHash;
- int isRunningHash;
- Camera _camera;
- Vector2 currentMovementInput;
- Vector3 currentMovement;
- Vector3 currentRunMovement;
- bool isMovementPressed;
- bool isRunPressed;
- float rotationFactorPerFrame = 15.0f;
- float runMultiplier = 3.0f;
- // Start is called before the first frame update
- void Start()
- {
- _camera = Camera.main;
- }
- void Awake()
- {
- playerInput = new PlayerInput();
- characterController = GetComponent<CharacterController>();
- animator = GetComponent<Animator>();
- isWalkingHash = Animator.StringToHash("isWalking");
- isRunningHash = Animator.StringToHash("isRunning");
- playerInput.CharacterControls.Move.started += onMovementInput;
- playerInput.CharacterControls.Move.canceled += onMovementInput;
- playerInput.CharacterControls.Move.performed += onMovementInput;
- playerInput.CharacterControls.Run.started += onRun;
- playerInput.CharacterControls.Run.canceled += onRun;
- }
- void onRun(InputAction.CallbackContext context)
- {
- isRunPressed = context.ReadValueAsButton();
- }
- void onMovementInput(InputAction.CallbackContext context)
- {
- currentMovementInput = context.ReadValue<Vector2>();
- currentMovement.x = currentMovementInput.x;
- currentMovement.z = currentMovementInput.y;
- currentRunMovement.x = currentMovementInput.x * runMultiplier;
- currentRunMovement.z = currentMovementInput.y * runMultiplier;
- isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
- }
- void handleAnimation()
- {
- bool isWalking = animator.GetBool(isWalkingHash);
- bool isRunning = animator.GetBool(isRunningHash);
- if (isMovementPressed && !isWalking)
- {
- animator.SetBool(isWalkingHash, true);
- }
- else if (!isMovementPressed && isWalking)
- {
- animator.SetBool(isWalkingHash, false);
- }
- if ((isMovementPressed && isRunPressed) && !isRunning)
- {
- animator.SetBool(isRunningHash, true);
- }
- else if ((!isMovementPressed && !isRunPressed) && isRunning)
- {
- animator.SetBool(isRunningHash, false);
- }
- else if ((isMovementPressed && !isRunPressed) && isRunning)
- {
- animator.SetBool(isRunningHash, false);
- }
- }
- void handleGravity()
- {
- if (characterController.isGrounded)
- {
- float groundedGravity = -0.05f;
- currentMovement.y = groundedGravity;
- currentRunMovement.y = groundedGravity;
- }
- else
- {
- float gravity = -9.8f;
- currentMovement.y = gravity;
- currentRunMovement.y = gravity;
- }
- }
- void handle_isRunPressed()
- {
- if (isRunPressed)
- {
- characterController.Move(currentRunMovement * Time.deltaTime);
- }
- else
- {
- characterController.Move(currentMovement * Time.deltaTime);
- }
- }
- void handleRotation()
- {
- // We're getting a Vector2, whereas we will need a Vector3
- // Get a z value based on camera, and include it in a Vector3
- Vector2 mousePosition = playerInput.CharacterControls.MousePosition.ReadValue<Vector2>();
- var mousePositionZ = _camera.farClipPlane * .5f;
- Vector3 mouseViewportPosition = _camera.ViewportToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, _camera.transform.position.y));
- Debug.Log("MousePos: " + mouseViewportPosition);
- Vector3 positionToLookAt;
- positionToLookAt.x = mouseViewportPosition.x;
- positionToLookAt.y = 0.0f;
- //positionToLookAt.z = currentMovement.z;
- positionToLookAt.z = mouseViewportPosition.z;
- Quaternion currentRotation = transform.rotation;
- Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt - transform.position);
- transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
- }
- float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
- {
- return Mathf.Atan2(b.y - a.y, b.x - a.x) * Mathf.Rad2Deg;
- }
- // Update is called once per frame
- void Update()
- {
- handleRotation();
- handleAnimation();
- handleGravity();
- handle_isRunPressed();
- }
- void OnEnable()
- {
- playerInput.CharacterControls.Enable();
- }
- void OnDisable()
- {
- playerInput.CharacterControls.Disable();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement