38 lines
1.4 KiB
C#
38 lines
1.4 KiB
C#
using UnityEngine;
|
|
using Code.Gameplay.Character.Controller.Input;
|
|
using KinematicCharacterController;
|
|
|
|
namespace Code.Gameplay.Character.Controller.Movement
|
|
{
|
|
public class AirMovementSystem
|
|
{
|
|
private readonly Vector3 _gravity;
|
|
private readonly float _maxAirSpeed;
|
|
private readonly float _airControlSharpness;
|
|
|
|
public AirMovementSystem(Vector3 gravity, float maxAirSpeed, float airControlSharpness)
|
|
{
|
|
_gravity = gravity;
|
|
_maxAirSpeed = maxAirSpeed;
|
|
_airControlSharpness = airControlSharpness;
|
|
}
|
|
|
|
public void UpdateVelocity(ref Vector3 currentVelocity, CharacterInputHandler inputHandler, KinematicCharacterMotor motor, float deltaTime)
|
|
{
|
|
currentVelocity += _gravity * deltaTime;
|
|
|
|
var horizontalVelocity = Vector3.ProjectOnPlane(currentVelocity, motor.CharacterUp);
|
|
var verticalVelocity = Vector3.Dot(currentVelocity, motor.CharacterUp) * motor.CharacterUp;
|
|
|
|
var desiredHorizontalMovement = Vector3.ProjectOnPlane(inputHandler.MoveInputVector, motor.CharacterUp).normalized * _maxAirSpeed;
|
|
|
|
horizontalVelocity = Vector3.Lerp(
|
|
horizontalVelocity,
|
|
desiredHorizontalMovement,
|
|
1f - Mathf.Exp(-_airControlSharpness * deltaTime)
|
|
);
|
|
|
|
currentVelocity = horizontalVelocity + verticalVelocity;
|
|
}
|
|
}
|
|
} |