Files
BABA_YAGA/Assets/Scripts/Player Controller/PlayerRunState.cs

68 lines
2.2 KiB
C#
Raw Normal View History

2026-03-26 20:27:19 +07:00
using UnityEngine;
namespace OnlyScove.Scripts
{
public class PlayerRunState : PlayerBaseState
{
public PlayerRunState(PlayerStateMachine stateMachine) : base(stateMachine) {}
public override void Enter()
{
2026-05-30 11:20:59 +07:00
stateMachine.AnimationHandler.SafeResetTrigger("Jump");
stateMachine.AnimationHandler.SafeResetTrigger("Fall");
stateMachine.AnimationHandler.ForceLocomotion();
2026-03-26 20:27:19 +07:00
stateMachine.Input.OnDodgeEvent += OnDodge;
stateMachine.Input.OnCrouchEvent += OnCrouch;
}
public override void Tick(float deltaTime)
{
2026-04-08 12:38:39 +07:00
Vector2 input = stateMachine.MoveInput;
2026-03-26 20:27:19 +07:00
float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y));
if (moveAmount <= 0.01f)
{
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
return;
}
2026-04-08 12:38:39 +07:00
if (!stateMachine.IsSprintHeld)
2026-03-26 20:27:19 +07:00
{
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
return;
}
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
2026-04-15 19:53:29 +07:00
Vector3 moveDirection = stateMachine.CameraRotation * inputDir;
2026-04-08 12:38:39 +07:00
moveDirection.y = 0;
moveDirection.Normalize();
2026-03-26 20:27:19 +07:00
Vector3 velocity = moveDirection * stateMachine.SprintSpeed;
if (stateMachine.IsGrounded && stateMachine.VelocityY < 0)
{
stateMachine.VelocityY = -2f;
}
else
{
2026-04-08 12:38:39 +07:00
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
2026-03-26 20:27:19 +07:00
}
velocity.y = stateMachine.VelocityY;
2026-04-08 12:38:39 +07:00
stateMachine.Move(velocity, 1.0f, deltaTime);
stateMachine.Rotate(moveDirection, deltaTime);
2026-03-26 20:27:19 +07:00
}
public override void PhysicsTick(float fixedDeltaTime) {}
public override void Exit()
{
stateMachine.Input.OnDodgeEvent -= OnDodge;
stateMachine.Input.OnCrouchEvent -= OnCrouch;
}
private void OnDodge() => stateMachine.SwitchState(new PlayerDodgeState(stateMachine));
private void OnCrouch() => stateMachine.SwitchState(new PlayerCrouchState(stateMachine));
}
}