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

51 lines
1.6 KiB
C#
Raw Normal View History

2026-04-08 12:38:39 +07:00
using UnityEngine;
2026-03-26 20:27:19 +07:00
namespace OnlyScove.Scripts
{
public class PlayerDodgeState : PlayerBaseState
{
private readonly int dodgeHash = Animator.StringToHash("Dodge");
2026-04-08 12:38:39 +07:00
private float dodgeDuration = 0.5f;
2026-03-26 20:27:19 +07:00
private float dodgeTimer;
private Vector3 dodgeDirection;
public PlayerDodgeState(PlayerStateMachine stateMachine) : base(stateMachine) {}
public override void Enter()
{
dodgeTimer = dodgeDuration;
2026-05-30 11:20:59 +07:00
stateMachine.AnimationHandler.SafeSetTrigger(dodgeHash);
2026-03-26 20:27:19 +07:00
2026-04-08 12:38:39 +07:00
Vector2 input = stateMachine.MoveInput;
2026-03-26 20:27:19 +07:00
if (input != Vector2.zero)
{
2026-04-08 12:38:39 +07:00
dodgeDirection = new Vector3(input.x, 0, input.y).normalized;
2026-04-15 19:53:29 +07:00
dodgeDirection = stateMachine.CameraRotation * dodgeDirection;
2026-03-26 20:27:19 +07:00
}
else
{
2026-04-08 12:38:39 +07:00
dodgeDirection = -stateMachine.transform.forward;
2026-03-26 20:27:19 +07:00
}
2026-04-08 12:38:39 +07:00
dodgeDirection.y = 0;
dodgeDirection.Normalize();
stateMachine.transform.rotation = Quaternion.LookRotation(dodgeDirection);
2026-03-26 20:27:19 +07:00
}
public override void Tick(float deltaTime)
{
dodgeTimer -= deltaTime;
2026-04-08 12:38:39 +07:00
// Sử dụng hàm Move tập trung
stateMachine.Move(dodgeDirection * (stateMachine.DashForce * 0.8f), 1.0f, deltaTime);
2026-03-26 20:27:19 +07:00
2026-04-08 12:38:39 +07:00
if (dodgeTimer <= 0)
2026-03-26 20:27:19 +07:00
{
2026-04-08 12:38:39 +07:00
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
2026-03-26 20:27:19 +07:00
}
}
public override void PhysicsTick(float fixedDeltaTime) {}
public override void Exit() {}
}
}