Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Drawing;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.ProBuilder.MeshOperations;
- public class PlayerMovement : MonoBehaviour
- {
- public float gravity = -13f;
- public float velocityY = 0f;
- [SerializeField] Transform orientation;
- [Header("Movement")]
- public float moveSpeed = 6f;
- [SerializeField] float airMultiplier = 0.4f;
- float movementMultiplier = 10f;
- [Header("Sprinting")]
- public float walkSpeed = 4f;
- [SerializeField] float crouchSpeed = 4f;
- [SerializeField] float sprintSpeed = 6f;
- [SerializeField] float acceleration = 10f;
- // public AnimationCurve accelTest;
- [HideInInspector] public float currentwalkSpeed = 0f;
- float currentcrouchSpeed = 0f;
- float currentsprintSpeed = 0f;
- CrouchScript crouchScript;
- [Header("Jumping")]
- public float jumpForce = 5f;
- float currentjumpForce;
- [Header("Keybinds")]
- [SerializeField] KeyCode sprintKey = KeyCode.LeftShift;
- [Header("Drag")]
- [SerializeField] float groundDrag = 6f;
- [SerializeField] float airDrag = 2f;
- Vector2 movementInput;
- [Header("Ground Detection")]
- [SerializeField] Transform groundCheck;
- [SerializeField] LayerMask groundMask;
- [SerializeField] float groundDistance = 0.2f;
- [Range(0.125f, 1f)]
- [SerializeField] float stairHeight = 0.35f;
- public bool isGrounded { get; private set; }
- bool isSprinting = false;
- [HideInInspector] public Vector3 moveDirection;
- Vector3 slopeMoveDirection;
- [HideInInspector] public Rigidbody rb;
- float slopeSpeedMult = 1.25f;
- RaycastHit slopeHit;
- public float currentStamina;
- float staminaLostRate = 0f;
- float maxStamina = 100;
- bool startStaminaRegain = false;
- [Header("Movement Helpers")]
- public float coyoteTime = 0.2f;
- float currentCoyoteTime = 0f;
- public float inputBufferTime = 0.1f;
- bool jumpBuffered = false;
- public float monkeyBoostAirDrag = 1.5f;
- // CrouchScript crouchRef;
- [HideInInspector] public KeyCode forwardKey = KeyCode.W;
- [HideInInspector] public KeyCode backKey = KeyCode.S;
- [HideInInspector] public KeyCode leftKey = KeyCode.A;
- [HideInInspector] public KeyCode rightKey = KeyCode.D;
- [HideInInspector] public KeyCode crouchKey = KeyCode.C;
- [HideInInspector] public KeyCode jumpKey = KeyCode.Space;
- [Header("Monky Stuff")]
- public float monkeyBoostCooldown = 5f; // Time you have to sprint to recharge monkey boost.
- float currentMonkeyBoostTime;
- public float forwardLeapForce = 5;
- public float upwardLeapForce = 20f;
- [HideInInspector] public bool monkeyBoostAvailable = true;
- [HideInInspector] public bool isMonkeyBoosting = false;
- [HideInInspector] public UnityEvent onMonkeyBoostReady;
- [HideInInspector] public UnityEvent onMonkeyBoost;
- bool readyingBoost;
- private void Start()
- {
- rb = GetComponent<Rigidbody>();
- crouchScript = GetComponent<CrouchScript>();
- rb.freezeRotation = true;
- currentwalkSpeed = walkSpeed;
- currentcrouchSpeed = crouchSpeed;
- currentsprintSpeed = sprintSpeed;
- currentjumpForce = jumpForce;
- currentStamina = maxStamina;
- currentCoyoteTime = coyoteTime;
- StartCoroutine(monkeyBoostTimer());
- // crouchRef = GetComponent<CrouchScript>();
- }
- private void Update()
- {
- isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
- // isGrounded = Physics.SphereCast(groundCheck.position, 0.25f, groundCheck.up * -1f, out, groundDistance, groundMask);
- MyInput();
- ControlDrag();
- ControlSpeed();
- if (startStaminaRegain == true && isSprinting)
- {
- startStaminaRegain = false;
- StopCoroutine(regainStamina());
- }
- else if (startStaminaRegain == false && currentStamina < maxStamina && !isSprinting)
- {
- startStaminaRegain = true;
- StartCoroutine(regainStamina());
- }
- if ((Input.GetKeyDown(jumpKey) && (isGrounded || currentCoyoteTime > 0)) || (isGrounded && jumpBuffered))
- {
- Jump();
- }
- else if (moveSpeed > walkSpeed && rb.velocity.magnitude >= 3f && monkeyBoostAvailable && Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.X)) MonkeyBoost();
- else if (Input.GetKeyDown(jumpKey) && !jumpBuffered && !isGrounded)
- {
- StartCoroutine(JumpBuffer());
- }
- // CheckForSteps();
- }
- void CheckForSteps()
- {
- if(Physics.Raycast(transform.position + transform.forward * 0.525f, Vector3.down, out RaycastHit feetHit, 1.5f, groundMask) && !isStepping)
- {
- if(MovingTowardsPoint(feetHit.point) && (feetHit.point.y - groundCheck.position.y) >= 0.05f)
- {
- Debug.Log("STEPPING UP");
- StepUp(feetHit.point);
- }
- }
- }
- private void FixedUpdate()
- {
- MovePlayer();
- }
- private void OnCollisionEnter(Collision collision)
- {
- if (collision.collider.isTrigger || !isGrounded) return;
- isMonkeyBoosting = false;
- if (velocityY >= -5f) return;
- StepUp(collision.GetContact(0).point);
- }
- bool isStepping = false;
- void StepUp(Vector3 point)
- {
- if (moveDirection == Vector3.zero || isStepping || OnSlope()) return;
- float difference = point.y - groundCheck.position.y;
- if (difference < stairHeight && difference >= 0.05f)
- {
- // rb.MovePosition(transform.position + (moveDirection.normalized * 0.5f + new Vector3(0, difference + 0.1f, 0)));
- // rb.AddForce(moveDirection.normalized * 1.25f, ForceMode.VelocityChange);
- StartCoroutine(stepUp(difference));
- }
- }
- IEnumerator stepUp(float difference)
- {
- isStepping = true;
- float stepTime = 0.1f;
- while(stepTime > 0)
- {
- stepTime -= Time.deltaTime;
- if(!OnSlope())rb.MovePosition(transform.position + (moveDirection.normalized * 0.5f + new Vector3(0, difference + 0.1f, 0)) * (20f * Time.deltaTime));
- else rb.MovePosition(transform.position + (slopeMoveDirection.normalized * 0.5f + new Vector3(0, difference + 0.1f, 0)) * (25f * Time.deltaTime));
- yield return null;
- }
- rb.AddForce(rb.velocity * 1.2f);
- isStepping = false;
- }
- void MyInput()
- {
- movementInput.x = Input.GetAxisRaw("Horizontal");
- movementInput.y = Input.GetAxisRaw("Vertical");
- isSprinting = Input.GetKey(sprintKey);
- /* if (horizontalMovement != 0 || verticalMovement != 0)
- {
- time += Time.deltaTime;
- }
- else time = 0; */
- moveDirection = (orientation.forward * movementInput.y + orientation.right * movementInput.x).normalized;
- //Debug.Log(Input.GetAxisRaw("Vertical"));
- slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
- }
- public IEnumerator primeMonkeyBoost()
- {
- bool doBoost = false;
- readyingBoost = true;
- yield return 3;
- float time = 0.2f;
- while(time > 0)
- {
- time -= Time.deltaTime;
- if(Input.GetKeyDown(jumpKey))
- {
- doBoost = true;
- break;
- }
- yield return null;
- }
- Debug.Log("Boosting: " + doBoost);
- if (doBoost) MonkeyBoost();
- readyingBoost = false;
- }
- public void MonkeyBoost()
- {
- onMonkeyBoost?.Invoke();
- isMonkeyBoosting = true;
- currentCoyoteTime = 0f;
- jumpBuffered = false;
- rb.velocity = new Vector3(rb.velocity.x, -3f, rb.velocity.z);
- rb.AddForce(transform.up * upwardLeapForce, ForceMode.Impulse);
- rb.AddForce(moveDirection * forwardLeapForce, ForceMode.Impulse);
- StartCoroutine(monkeyBoostTimer());
- }
- IEnumerator monkeyBoostTimer()
- {
- monkeyBoostAvailable = false;
- yield return new WaitForSeconds(monkeyBoostCooldown);
- onMonkeyBoostReady?.Invoke();
- monkeyBoostAvailable = true;
- isMonkeyBoosting = false;
- }
- public void FillMonkeyBoostBar()
- {
- Debug.Log("OOO OOO AH AH");
- StopCoroutine(monkeyBoostTimer());
- onMonkeyBoostReady?.Invoke();
- monkeyBoostAvailable = true;
- isMonkeyBoosting = false;
- }
- void Jump()
- {
- currentCoyoteTime = 0f;
- jumpBuffered = false;
- rb.velocity = new Vector3(rb.velocity.x, -3f, rb.velocity.z);
- rb.AddForce(transform.up * currentjumpForce, ForceMode.Impulse);
- }
- IEnumerator JumpBuffer()
- {
- jumpBuffered = true;
- yield return new WaitForSeconds(inputBufferTime);
- jumpBuffered = false;
- }
- void ControlSpeed()
- {
- /* if (crouchRef.isCrouching && isGrounded)
- {
- moveSpeed = Mathf.Lerp(moveSpeed, currentcrouchSpeed, acceleration * Time.deltaTime);
- } */
- if (!isGrounded)
- {
- moveSpeed = Mathf.Lerp(moveSpeed, currentwalkSpeed * 0.7f, 5f * Time.deltaTime);
- }
- else if (crouchScript.isCrouching)
- {
- moveSpeed = Mathf.Lerp(moveSpeed, currentcrouchSpeed, acceleration * Time.deltaTime);
- }
- else if (Input.GetKey(sprintKey) && isGrounded && currentStamina > 0)
- {
- moveSpeed = Mathf.Lerp(moveSpeed, currentsprintSpeed, acceleration * Time.deltaTime);
- currentStamina -= staminaLostRate * Time.fixedDeltaTime;
- startStaminaRegain = false;
- }
- else
- {
- moveSpeed = Mathf.Lerp(moveSpeed, currentwalkSpeed, acceleration * Time.deltaTime);
- // moveSpeed = accelTest.Evaluate(time);
- }
- }
- IEnumerator regainStamina()
- {
- yield return new WaitForSeconds(2f);
- while(currentStamina < maxStamina)
- {
- currentStamina += Time.fixedDeltaTime * 7.5f;
- if (isSprinting)
- {
- startStaminaRegain = false;
- yield break;
- }
- yield return null;
- }
- currentStamina = maxStamina;
- startStaminaRegain = false;
- }
- void ControlDrag()
- {
- if (isGrounded)
- {
- rb.drag = groundDrag;
- }
- else if (!isMonkeyBoosting)
- {
- rb.drag = airDrag;
- }
- else rb.drag = monkeyBoostAirDrag;
- }
- public void ChangeMobility(float amount)
- {
- currentcrouchSpeed = crouchSpeed * amount;
- currentwalkSpeed = walkSpeed * amount;
- currentsprintSpeed = sprintSpeed * amount;
- currentjumpForce = jumpForce * amount;
- }
- public void ChangeMobility(float amount, bool isInfinite = false, float timeActive = 10f)
- {
- currentcrouchSpeed = crouchSpeed * amount;
- currentwalkSpeed = walkSpeed * amount;
- currentsprintSpeed = sprintSpeed * amount;
- if (isInfinite == false) Invoke("RevertSpeed", timeActive);
- }
- public void RevertSpeed()
- {
- currentcrouchSpeed = crouchSpeed;
- currentwalkSpeed = walkSpeed;
- currentsprintSpeed = sprintSpeed;
- currentjumpForce = jumpForce;
- }
- void MovePlayer()
- {
- if (isGrounded && OnSlope())
- {
- velocityY = 0f;
- PreventSlopeSlide();
- rb.AddForce(slopeMoveDirection * moveSpeed * slopeSpeedMult * movementMultiplier, ForceMode.Acceleration);
- }
- else if (isGrounded && !OnSlope())
- {
- velocityY = 0f;
- rb.AddForce(moveDirection * moveSpeed * movementMultiplier, ForceMode.Acceleration);
- }
- else if (!isGrounded)
- {
- velocityY += gravity * Time.deltaTime;
- rb.AddForce((moveDirection * moveSpeed * movementMultiplier * airMultiplier) + Vector3.up * velocityY, ForceMode.Acceleration);
- }
- // rb.useGravity = !OnSlope();
- }
- void PreventSlopeSlide()
- {
- if (slopeMoveDirection == Vector3.zero) GetComponent<Collider>().material.dynamicFriction = Mathf.Lerp(GetComponent<Collider>().material.dynamicFriction, 0.8f, 3f * Time.deltaTime);
- else GetComponent<Collider>().material.dynamicFriction = 0;
- }
- private bool OnSlope()
- {
- if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, 1.25f, groundMask))
- {
- float slopeAngle = Vector3.Angle(slopeHit.normal, Vector3.up);
- if(slopeAngle > 0)
- {
- float angleLerp = Mathf.InverseLerp(35f, 65f, slopeAngle);
- slopeSpeedMult = 1.05f;
- return true;
- }
- /* if (slopeAngle >= 45f)
- {
- slopeSpeedMult = 1.35f;
- return true;
- }
- else if (slopeAngle >= 30f)
- {
- slopeSpeedMult = 1.2f;
- return true;
- }
- else if (slopeAngle >= 15f)
- {
- slopeSpeedMult = 1.15f;
- return true;
- } */
- return false;
- }
- return false;
- }
- private void OnDisable()
- {
- moveDirection = Vector3.zero;
- }
- private void OnValidate()
- {
- RevertSpeed();
- }
- private void OnDrawGizmos()
- {
- if (groundCheck != null) Gizmos.DrawSphere(groundCheck.position, groundDistance);
- }
- public void ResetGravity()
- {
- velocityY = 0;
- }
- // WHy does this work? No Idea.
- bool MovingTowardsPoint(Vector3 point)
- {
- Vector3 dirToTarget = Vector3.Normalize(point - transform.position);
- float dot = Vector3.Dot(moveDirection, new Vector3(dirToTarget.x, 0, dirToTarget.z));
- if (dot >= 0.2f) return true;
- else return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment