Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class Movement : MonoBehaviour
- {
- public float playerSpeed = 1f;
- public float jumpForce = 1f;
- public float gravityValue = 1f;
- public float friction = 1f;
- public float cameraSensitivity = 1f;
- private float currentYRotation = 0f;
- public CharacterController CharacterController;
- private Vector3 playerVelocity;
- [SerializeField] private bool isGrounded;
- public InputActionAsset playerControls;
- private InputAction moveAction;
- private InputAction lookAction;
- private InputAction jumpAction;
- private Vector3 MoveInput;
- private Vector2 LookInput;
- private bool JumpTrigger;
- public Transform cameraTransform;
- private void Awake()
- {
- moveAction = playerControls.FindActionMap("Player").FindAction("Movement");
- lookAction = playerControls.FindActionMap("Player").FindAction("Look");
- jumpAction = playerControls.FindActionMap("Player").FindAction("Jump");
- RegisterInputActions();
- }
- private void RegisterInputActions()
- {
- moveAction.performed += context => MoveInput = new Vector3(context.ReadValue<Vector2>().x,0 , context.ReadValue<Vector2>().y);
- moveAction.canceled += context => MoveInput = Vector3.zero;
- lookAction.performed += context => LookInput = context.ReadValue<Vector2>();
- lookAction.canceled += context => LookInput = Vector2.zero;
- jumpAction.performed += context => JumpTrigger = true;
- jumpAction.canceled += context => JumpTrigger = false;
- }
- private void OnEnable()
- {
- moveAction.Enable();
- lookAction.Enable();
- jumpAction.Enable();
- }
- private void OnDisable()
- {
- moveAction.Disable();
- lookAction.Disable();
- jumpAction.Disable();
- }
- private void Update()
- {
- PlayerJumping();
- PlayerCameraMovement();
- PlayerHorizontalMovement();
- //UpdateFriction();
- CharacterController.Move(playerVelocity * Time.deltaTime);
- }
- private void PlayerCameraMovement()
- {
- currentYRotation += LookInput.x * Time.deltaTime * cameraSensitivity;
- transform.rotation = Quaternion.Euler(0, currentYRotation, 0);
- }
- private void PlayerHorizontalMovement()
- {
- if (!isGrounded)
- return;
- Vector2 input = new Vector2(MoveInput.x, MoveInput.z);
- input = Vector2.ClampMagnitude(input, 1f);
- Vector3 forward = cameraTransform.forward;
- Vector3 right = cameraTransform.right;
- forward.y = 0;
- right.y = 0;
- forward.Normalize();
- right.Normalize();
- Vector3 moveDirection = (forward * input.y + right * input.x);
- playerVelocity.x = moveDirection.x * playerSpeed;
- playerVelocity.z = moveDirection.z * playerSpeed;
- int value;
- if (Math.Abs(playerVelocity.x) > 3)
- playerVelocity.x = 2.9f * (value = playerVelocity.x < 0 ? -1 : 1);
- if (Math.Abs(playerVelocity.z) > 3)
- playerVelocity.z = 2.9f * (value = playerVelocity.z < 0 ? -1 : 1);
- }
- private void UpdateFriction()
- {
- if (!isGrounded)
- {
- friction = 0.01f;
- }
- else
- friction = 0.07f; // Няма friction в !isGrounded
- }
- private void PlayerJumping()
- {
- isGrounded = CharacterController.isGrounded;
- if (isGrounded)
- {
- playerVelocity.y = -0.5f;
- if (JumpTrigger)
- playerVelocity.y = jumpForce;
- }
- else
- {
- playerVelocity.y -= gravityValue * Time.deltaTime;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment