51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using UnityEngine;
|
|
|
|
namespace OnlyScove.Scripts
|
|
{
|
|
public class PlayerDodgeState : PlayerBaseState
|
|
{
|
|
private readonly int dodgeHash = Animator.StringToHash("Dodge");
|
|
private float dodgeDuration = 0.5f;
|
|
private float dodgeTimer;
|
|
private Vector3 dodgeDirection;
|
|
|
|
public PlayerDodgeState(PlayerStateMachine stateMachine) : base(stateMachine) {}
|
|
|
|
public override void Enter()
|
|
{
|
|
dodgeTimer = dodgeDuration;
|
|
stateMachine.AnimationHandler.SafeSetTrigger(dodgeHash);
|
|
|
|
Vector2 input = stateMachine.MoveInput;
|
|
if (input != Vector2.zero)
|
|
{
|
|
dodgeDirection = new Vector3(input.x, 0, input.y).normalized;
|
|
dodgeDirection = stateMachine.CameraRotation * dodgeDirection;
|
|
}
|
|
else
|
|
{
|
|
dodgeDirection = -stateMachine.transform.forward;
|
|
}
|
|
dodgeDirection.y = 0;
|
|
dodgeDirection.Normalize();
|
|
|
|
stateMachine.transform.rotation = Quaternion.LookRotation(dodgeDirection);
|
|
}
|
|
|
|
public override void Tick(float deltaTime)
|
|
{
|
|
dodgeTimer -= deltaTime;
|
|
|
|
// Sử dụng hàm Move tập trung
|
|
stateMachine.Move(dodgeDirection * (stateMachine.DashForce * 0.8f), 1.0f, deltaTime);
|
|
|
|
if (dodgeTimer <= 0)
|
|
{
|
|
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
|
}
|
|
}
|
|
|
|
public override void PhysicsTick(float fixedDeltaTime) {}
|
|
public override void Exit() {}
|
|
}
|
|
} |