UnityBiz/Assets/scripts/Movement/PlayerMovementController.Slide.cs
adamwi1000 b854205dcb test
2024-08-02 06:41:41 +02:00

74 lines
2.2 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
namespace Player.Movement
{
public partial class PlayerMovementController
{
public bool IsSliding { get; private set; }
private float _originalFrictionCoefficient;
private bool successfulSlide = false;
private void SlideEnd(InputAction.CallbackContext obj)
{
StopSlide();
}
private void SlideSpeedCheck()
{
if (!IsSliding) return;
if (_rigidbody.velocity.magnitude < 1.5f)
{
StopSlide();
}
}
private void StopSlide()
{
if (!successfulSlide) return;
successfulSlide = false;
IsSliding = false;
frictionCoefficient = _originalFrictionCoefficient;
}
private bool DeadSlideCheck()
{
var playerMovementInput = moveAction.action.ReadValue<Vector2>();
var playerVel = _rigidbody.velocity.magnitude;
if (playerVel < maxMovementSpeed / 2) return true;
var cameraRotationVector = playerCameraController.CameraRotationVector;
cameraRotationVector = new Vector2(0, cameraRotationVector.y);
var cameraRotation = Quaternion.Euler(cameraRotationVector);
var movementDirection = new Vector3(playerMovementInput.x, 0, playerMovementInput.y);
var desiredMovementDirection = cameraRotation * movementDirection;
return Vector3.Dot(_rigidbody.velocity.normalized, desiredMovementDirection.normalized) < 0.71f;
}
private void SlideStart(InputAction.CallbackContext obj)
{
if (!IsGrounded) return;
successfulSlide = true;
IsSliding = true;
_originalFrictionCoefficient = frictionCoefficient;
frictionCoefficient = slideFrictionCoefficient;
// dead slide
if (DeadSlideCheck()) return;
var diff = Mathf.Max(slideSpeed - _rigidbody.velocity.magnitude, 0);
var force = _rigidbody.velocity.normalized * diff;
force.y = 0;
_rigidbody.AddForce(force, ForceMode.Impulse);
}
}
}