martigpg3

Movement script - BACKUP

Jun 15th, 2026
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.InputSystem;
  6.  
  7. public class Movement : MonoBehaviour
  8. {
  9.     public float playerSpeed = 1f;
  10.     public float jumpForce = 1f;
  11.     public float gravityValue = 1f;
  12.     public float friction = 1f;
  13.     public float cameraSensitivity = 1f;
  14.  
  15.     private float currentYRotation = 0f;
  16.  
  17.     public CharacterController CharacterController;
  18.     private Vector3 playerVelocity;
  19.     [SerializeField] private bool isGrounded;
  20.  
  21.     public InputActionAsset playerControls;
  22.  
  23.     private InputAction moveAction;
  24.     private InputAction lookAction;
  25.     private InputAction jumpAction;
  26.  
  27.     private Vector3 MoveInput;
  28.     private Vector2 LookInput;
  29.     private bool JumpTrigger;
  30.  
  31.     public Transform cameraTransform;
  32.  
  33.     private void Awake()
  34.     {
  35.         moveAction = playerControls.FindActionMap("Player").FindAction("Movement");
  36.         lookAction = playerControls.FindActionMap("Player").FindAction("Look");
  37.         jumpAction = playerControls.FindActionMap("Player").FindAction("Jump");
  38.         RegisterInputActions();
  39.     }
  40.     private void RegisterInputActions()
  41.     {
  42.         moveAction.performed += context => MoveInput = new Vector3(context.ReadValue<Vector2>().x,0 , context.ReadValue<Vector2>().y);
  43.         moveAction.canceled += context => MoveInput = Vector3.zero;
  44.  
  45.         lookAction.performed += context => LookInput = context.ReadValue<Vector2>();
  46.         lookAction.canceled += context => LookInput = Vector2.zero;
  47.  
  48.         jumpAction.performed += context => JumpTrigger = true;
  49.         jumpAction.canceled += context => JumpTrigger = false;
  50.     }
  51.  
  52.     private void OnEnable()
  53.     {
  54.         moveAction.Enable();
  55.         lookAction.Enable();
  56.         jumpAction.Enable();
  57.     }
  58.  
  59.     private void OnDisable()
  60.     {
  61.         moveAction.Disable();
  62.         lookAction.Disable();
  63.         jumpAction.Disable();
  64.     }
  65.  
  66.     private void Update()
  67.     {
  68.         PlayerJumping();
  69.         PlayerCameraMovement();
  70.         PlayerHorizontalMovement();
  71.         //UpdateFriction();
  72.  
  73.         CharacterController.Move(playerVelocity * Time.deltaTime);
  74.     }
  75.  
  76.     private void PlayerCameraMovement()
  77.     {
  78.         currentYRotation += LookInput.x * Time.deltaTime * cameraSensitivity;
  79.         transform.rotation = Quaternion.Euler(0, currentYRotation, 0);
  80.     }
  81.  
  82.     private void PlayerHorizontalMovement()
  83.     {
  84.         if (!isGrounded)
  85.             return;
  86.  
  87.         Vector2 input = new Vector2(MoveInput.x, MoveInput.z);
  88.         input = Vector2.ClampMagnitude(input, 1f);
  89.  
  90.         Vector3 forward = cameraTransform.forward;
  91.         Vector3 right = cameraTransform.right;
  92.  
  93.         forward.y = 0;
  94.         right.y = 0;
  95.  
  96.         forward.Normalize();
  97.         right.Normalize();
  98.  
  99.         Vector3 moveDirection = (forward * input.y + right * input.x);
  100.  
  101.         playerVelocity.x = moveDirection.x * playerSpeed;
  102.         playerVelocity.z = moveDirection.z * playerSpeed;
  103.  
  104.         int value;
  105.         if (Math.Abs(playerVelocity.x) > 3)
  106.             playerVelocity.x = 2.9f * (value = playerVelocity.x < 0 ? -1 : 1);
  107.  
  108.         if (Math.Abs(playerVelocity.z) > 3)
  109.             playerVelocity.z = 2.9f * (value = playerVelocity.z < 0 ? -1 : 1);
  110.     }
  111.  
  112.     private void UpdateFriction()
  113.     {
  114.         if (!isGrounded)
  115.         {
  116.             friction = 0.01f;
  117.         }
  118.         else
  119.             friction = 0.07f; // Няма friction в !isGrounded
  120.     }
  121.  
  122.     private void PlayerJumping()
  123.     {
  124.         isGrounded = CharacterController.isGrounded;
  125.  
  126.         if (isGrounded)
  127.         {
  128.             playerVelocity.y = -0.5f;
  129.  
  130.             if (JumpTrigger)
  131.                 playerVelocity.y = jumpForce;
  132.  
  133.         }
  134.         else
  135.         {
  136.             playerVelocity.y -= gravityValue * Time.deltaTime;
  137.         }
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment