Advertisement
360407

Player Movement Code

Jan 21st, 2025 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | Source Code | 0 0
  1. Current Version: V0.03
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class PlayerMovement : MonoBehaviour
  8. {
  9.     [Header("Movement")]
  10.     private float moveSpeed;
  11.     public float walkSpeed;
  12.     public float sprintSpeed;
  13.  
  14.     public float groundDrag;
  15.  
  16.     [Header("GroundCheck")]
  17.     public float playerHeight;
  18.     public LayerMask whatIsGround;
  19.     bool grounded;
  20.  
  21.     [Header("Jumping")]
  22.     public float jumpForce;
  23.     public float jumpCooldown;
  24.     public float airMultiplier;
  25.     bool readyToJump;
  26.  
  27.     [Header("Keybindings")]
  28.     public KeyCode jumpKey = KeyCode.Space;
  29.     public KeyCode sprintKey = KeyCode.LeftShift;
  30.  
  31.     public Transform orientation;
  32.  
  33.     float horizontalInput;
  34.     float verticalInput;
  35.  
  36.     Vector3 moveDirection;
  37.  
  38.     Rigidbody rb;
  39.  
  40.     public MovementState state;
  41.  
  42.     public enum MovementState
  43.     {
  44.         walking,
  45.         sprinting,
  46.         air,
  47.     }
  48.  
  49.     private void Start()
  50.     {
  51.         //Assign rigidbody and freeze its rotaion
  52.         rb = GetComponent<Rigidbody>();
  53.         rb.freezeRotation = true;
  54.  
  55.         readyToJump = true;
  56.     }
  57.     private void Update()
  58.     {
  59.         //Call Functions
  60.         MovementInputs();
  61.         SpeedControl();
  62.         StatesHandler();
  63.         //DynamicFOV();
  64.  
  65.         //Check if player is on ground / Raycast down from half the players height (0.5 in this case) + little extra (0.2)
  66.         grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
  67.  
  68.         //Handle drag
  69.         if (grounded)
  70.         {
  71.             rb.drag = groundDrag;
  72.         }
  73.         else
  74.         {
  75.             rb.drag = 0;
  76.         }
  77.     }
  78.  
  79.     private void FixedUpdate()
  80.     {
  81.         //Call MovePlayer Function
  82.         MovePlayer();
  83.     }
  84.     private void MovementInputs()
  85.     {
  86.         //Assign vertical and horizontal inputs
  87.         horizontalInput = Input.GetAxisRaw("Horizontal");
  88.         verticalInput = Input.GetAxisRaw("Vertical");
  89.     }
  90.  
  91.     private void MovePlayer()
  92.     {
  93.         //Calculate the move direction / Walk in the direction your looking
  94.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
  95.  
  96.         //On ground - Moves the player forward based on the moveSpeed number * 10
  97.         if (grounded)
  98.         {
  99.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
  100.         }
  101.         else if (!grounded)
  102.         {
  103.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
  104.         }
  105.     }
  106.  
  107.     private void SpeedControl()
  108.     {
  109.         Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
  110.  
  111.         //Limit velocity
  112.         if (flatVel.magnitude > moveSpeed) //If you go faster than moveSpeed
  113.         {
  114.             Vector3 limitedVel = flatVel.normalized * moveSpeed; //Calculate max vel
  115.             rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z); //Apply it
  116.         }
  117.     }
  118.  
  119.     private void StatesHandler()
  120.     {
  121.         //Sprinting - If sprint key is pressed State = Sprinitng
  122.         if (grounded && Input.GetKey(sprintKey))
  123.         {
  124.             state = MovementState.sprinting;
  125.             moveSpeed = sprintSpeed;
  126.         }
  127.         //Otherwise state = walking
  128.         else if (grounded)
  129.         {
  130.             state = MovementState.walking;
  131.             moveSpeed = walkSpeed;
  132.         }
  133.     }
  134. }
Tags: C# csharp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement