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

52 lines
1.6 KiB
C#
Raw Normal View History

2026-03-26 20:27:19 +07:00
using UnityEngine;
namespace OnlyScove.Scripts
{
public class PlayerJumpState : PlayerBaseState
{
private readonly int jumpHash = Animator.StringToHash("Jump");
private float jumpSpeed;
public PlayerJumpState(PlayerStateMachine stateMachine, float jumpSpeed = -1f) : base(stateMachine)
{
if (jumpSpeed < 0)
{
this.jumpSpeed = stateMachine.WalkSpeed;
}
else
{
this.jumpSpeed = jumpSpeed;
}
}
public override void Enter()
{
stateMachine.Anim.SetTrigger(jumpHash);
// Physic formula: v = sqrt(h * -2 * g)
stateMachine.VelocityY = Mathf.Sqrt(stateMachine.JumpHeight * -2f * Physics.gravity.y);
}
public override void Tick(float deltaTime)
{
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
Vector2 input = stateMachine.Input.MoveInput;
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
Vector3 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir;
Vector3 velocity = moveDirection * jumpSpeed;
velocity.y = stateMachine.VelocityY;
stateMachine.Controller.Move(velocity * deltaTime);
if (stateMachine.VelocityY <= 0f)
{
stateMachine.SwitchState(new PlayerFallState(stateMachine, jumpSpeed));
}
}
public override void PhysicsTick(float fixedDeltaTime) {}
public override void Exit() {}
}
}