mirror of
https://github.com/Project-Redacted/project-hampter.git
synced 2025-05-29 23:03:16 +00:00
108 lines
2.4 KiB
C#
108 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Sliding : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
public Transform orientation;
|
|
public Transform playerObj;
|
|
private Rigidbody rb;
|
|
private PlayerMovement pm;
|
|
PlayerStamina playerStamina;
|
|
[Header("Sliding")]
|
|
public float maxSlideTime;
|
|
public float slideForce;
|
|
private float slideTimer;
|
|
|
|
|
|
|
|
public float slideYScale;
|
|
private float startYScale;
|
|
|
|
[Header("Input")]
|
|
public KeyCode slideKey = KeyCode.LeftControl;
|
|
private float horizontalInput;
|
|
private float verticalInput;
|
|
|
|
|
|
public float slideStaminaCost = 20;
|
|
|
|
private void Start()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
pm = GetComponent<PlayerMovement>();
|
|
playerStamina = GetComponent<PlayerStamina>();
|
|
|
|
startYScale = playerObj.localScale.y;
|
|
|
|
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
horizontalInput = Input.GetAxisRaw("Horizontal");
|
|
verticalInput = Input.GetAxisRaw("Vertical");
|
|
|
|
if (Input.GetKeyDown(slideKey) && (horizontalInput != 0 || verticalInput != 0))
|
|
StartSlide();
|
|
|
|
if (Input.GetKeyUp(slideKey) && pm.sliding)
|
|
StopSlide();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (pm.sliding)
|
|
SlidingMovement();
|
|
}
|
|
|
|
|
|
|
|
private void StartSlide()
|
|
{
|
|
|
|
if (!playerStamina.DecreaseByChunk(slideStaminaCost))
|
|
{
|
|
return;
|
|
}
|
|
pm.sliding = true;
|
|
|
|
playerObj.localScale = new Vector3(playerObj.localScale.x, slideYScale, playerObj.localScale.z);
|
|
rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
|
|
|
|
slideTimer = maxSlideTime;
|
|
}
|
|
|
|
private void SlidingMovement()
|
|
{
|
|
playerStamina.DecreaseStamina();
|
|
|
|
Vector3 inputDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
|
|
|
|
if(!pm.OnSlope() || rb.velocity.y > -0.1f)
|
|
{
|
|
rb.AddForce(inputDirection.normalized * slideForce, ForceMode.Force);
|
|
slideTimer -= Time.deltaTime;
|
|
}
|
|
|
|
else
|
|
{
|
|
rb.AddForce(pm.GetSlopeMoveDirection(inputDirection) * slideForce, ForceMode.Force);
|
|
}
|
|
|
|
|
|
if (slideTimer <= 0)
|
|
StopSlide();
|
|
}
|
|
|
|
private void StopSlide()
|
|
{
|
|
pm.sliding = false;
|
|
|
|
playerObj.localScale = new Vector3(playerObj.localScale.x, startYScale, playerObj.localScale.z);
|
|
}
|
|
|
|
|
|
|
|
}
|