Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Current Version: V0.03
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- [Header("Movement")]
- private float moveSpeed;
- public float walkSpeed;
- public float sprintSpeed;
- public float groundDrag;
- [Header("GroundCheck")]
- public float playerHeight;
- public LayerMask whatIsGround;
- bool grounded;
- [Header("Jumping")]
- public float jumpForce;
- public float jumpCooldown;
- public float airMultiplier;
- bool readyToJump;
- [Header("Keybindings")]
- public KeyCode jumpKey = KeyCode.Space;
- public KeyCode sprintKey = KeyCode.LeftShift;
- public Transform orientation;
- float horizontalInput;
- float verticalInput;
- Vector3 moveDirection;
- Rigidbody rb;
- public MovementState state;
- public enum MovementState
- {
- walking,
- sprinting,
- air,
- }
- private void Start()
- {
- //Assign rigidbody and freeze its rotaion
- rb = GetComponent<Rigidbody>();
- rb.freezeRotation = true;
- readyToJump = true;
- }
- private void Update()
- {
- //Call Functions
- MovementInputs();
- SpeedControl();
- StatesHandler();
- //DynamicFOV();
- //Check if player is on ground / Raycast down from half the players height (0.5 in this case) + little extra (0.2)
- grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
- //Handle drag
- if (grounded)
- {
- rb.drag = groundDrag;
- }
- else
- {
- rb.drag = 0;
- }
- }
- private void FixedUpdate()
- {
- //Call MovePlayer Function
- MovePlayer();
- }
- private void MovementInputs()
- {
- //Assign vertical and horizontal inputs
- horizontalInput = Input.GetAxisRaw("Horizontal");
- verticalInput = Input.GetAxisRaw("Vertical");
- }
- private void MovePlayer()
- {
- //Calculate the move direction / Walk in the direction your looking
- moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
- //On ground - Moves the player forward based on the moveSpeed number * 10
- if (grounded)
- {
- rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
- }
- else if (!grounded)
- {
- rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
- }
- }
- private void SpeedControl()
- {
- Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
- //Limit velocity
- if (flatVel.magnitude > moveSpeed) //If you go faster than moveSpeed
- {
- Vector3 limitedVel = flatVel.normalized * moveSpeed; //Calculate max vel
- rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z); //Apply it
- }
- }
- private void StatesHandler()
- {
- //Sprinting - If sprint key is pressed State = Sprinitng
- if (grounded && Input.GetKey(sprintKey))
- {
- state = MovementState.sprinting;
- moveSpeed = sprintSpeed;
- }
- //Otherwise state = walking
- else if (grounded)
- {
- state = MovementState.walking;
- moveSpeed = walkSpeed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement