65 lines
2.3 KiB
C#
65 lines
2.3 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace OnlyScove.Scripts
|
|||
|
|
{
|
|||
|
|
public class PlayerDodgeState : PlayerBaseState
|
|||
|
|
{
|
|||
|
|
private readonly int dodgeHash = Animator.StringToHash("Dodge");
|
|||
|
|
private float dodgeDuration = 0.4f; // Adjust based on your dodge animation length
|
|||
|
|
private float dodgeTimer;
|
|||
|
|
private Vector3 dodgeDirection;
|
|||
|
|
|
|||
|
|
public PlayerDodgeState(PlayerStateMachine stateMachine) : base(stateMachine) {}
|
|||
|
|
|
|||
|
|
public override void Enter()
|
|||
|
|
{
|
|||
|
|
dodgeTimer = dodgeDuration;
|
|||
|
|
stateMachine.Anim.SetTrigger(dodgeHash);
|
|||
|
|
|
|||
|
|
// Calculate dodge direction based on current input
|
|||
|
|
Vector2 input = stateMachine.Input.MoveInput;
|
|||
|
|
if (input != Vector2.zero)
|
|||
|
|
{
|
|||
|
|
// Dodge in the input direction (Left, Right, Back, Forward)
|
|||
|
|
dodgeDirection = new Vector3(input.x, 0f, input.y).normalized;
|
|||
|
|
|
|||
|
|
if (stateMachine.Cam != null)
|
|||
|
|
{
|
|||
|
|
dodgeDirection = stateMachine.Cam.PlanarRotation * dodgeDirection;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Instantly rotate the player to face the dodge direction
|
|||
|
|
stateMachine.transform.rotation = Quaternion.LookRotation(dodgeDirection);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// If no input, just roll straight forward
|
|||
|
|
dodgeDirection = stateMachine.transform.forward;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override void Tick(float deltaTime)
|
|||
|
|
{
|
|||
|
|
dodgeTimer -= deltaTime;
|
|||
|
|
|
|||
|
|
// Apply movement force for the dodge (usually slightly slower than a Dash)
|
|||
|
|
stateMachine.Controller.Move(dodgeDirection * (stateMachine.DashForce * 0.8f) * deltaTime);
|
|||
|
|
|
|||
|
|
// Once the roll finishes, transition back to Idle or Move
|
|||
|
|
if (dodgeTimer <= 0f)
|
|||
|
|
{
|
|||
|
|
if (stateMachine.Input.MoveInput != Vector2.zero)
|
|||
|
|
{
|
|||
|
|
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override void PhysicsTick(float fixedDeltaTime) {}
|
|||
|
|
public override void Exit() {}
|
|||
|
|
}
|
|||
|
|
}
|