update jump
This commit is contained in:
@@ -5,6 +5,10 @@ namespace OnlyScove.Scripts
|
||||
public class PlayerJumpState : PlayerBaseState
|
||||
{
|
||||
private readonly int jumpHash = Animator.StringToHash("Jump");
|
||||
private readonly int speedHash = Animator.StringToHash("Speed");
|
||||
private readonly int speedXHash = Animator.StringToHash("Velocity X");
|
||||
private readonly int speedZHash = Animator.StringToHash("Velocity Z");
|
||||
|
||||
private float jumpSpeed;
|
||||
|
||||
public PlayerJumpState(PlayerStateMachine stateMachine, float jumpSpeed = -1f) : base(stateMachine)
|
||||
@@ -21,10 +25,22 @@ namespace OnlyScove.Scripts
|
||||
|
||||
public override void Enter()
|
||||
{
|
||||
// Set initial velocity for the Jump Blend Tree (2D Freeform)
|
||||
Vector2 input = stateMachine.Input.MoveInput;
|
||||
stateMachine.Anim.SetFloat(speedXHash, input.x);
|
||||
stateMachine.Anim.SetFloat(speedZHash, input.y);
|
||||
|
||||
// Set Speed parameter to help distinguish between Idle Jump (0) and Run Jump (0.7+)
|
||||
float moveAmount = input.magnitude;
|
||||
stateMachine.Anim.SetFloat(speedHash, moveAmount > 0.1f ? 1.0f : 0f);
|
||||
|
||||
stateMachine.Anim.ResetTrigger(jumpHash);
|
||||
stateMachine.Anim.SetTrigger(jumpHash);
|
||||
|
||||
// Physic formula: v = sqrt(h * -2 * g)
|
||||
stateMachine.VelocityY = Mathf.Sqrt(stateMachine.JumpHeight * -2f * Physics.gravity.y);
|
||||
|
||||
stateMachine.Input.OnSprintEvent += OnAirDash;
|
||||
}
|
||||
|
||||
public override void Tick(float deltaTime)
|
||||
@@ -40,13 +56,37 @@ namespace OnlyScove.Scripts
|
||||
|
||||
stateMachine.Controller.Move(velocity * deltaTime);
|
||||
|
||||
// Cập nhật Animator cho việc di chuyển trên không (Blend Tree sẽ tự mượt mà theo các giá trị này)
|
||||
float multiplier = stateMachine.Input.IsSprintHeld ? 1f : 0.5f;
|
||||
stateMachine.Anim.SetFloat(speedXHash, input.x * multiplier, stateMachine.AnimationDamping, deltaTime);
|
||||
stateMachine.Anim.SetFloat(speedZHash, input.y * multiplier, stateMachine.AnimationDamping, deltaTime);
|
||||
|
||||
if (moveDirection != Vector3.zero)
|
||||
{
|
||||
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
||||
stateMachine.transform.rotation = Quaternion.RotateTowards(
|
||||
stateMachine.transform.rotation,
|
||||
targetRot,
|
||||
stateMachine.RotationSpeed * deltaTime
|
||||
);
|
||||
}
|
||||
|
||||
if (stateMachine.VelocityY <= 0f)
|
||||
{
|
||||
stateMachine.SwitchState(new PlayerFallState(stateMachine, jumpSpeed));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAirDash()
|
||||
{
|
||||
stateMachine.SwitchState(new PlayerAirDashState(stateMachine));
|
||||
}
|
||||
|
||||
public override void PhysicsTick(float fixedDeltaTime) {}
|
||||
public override void Exit() {}
|
||||
|
||||
public override void Exit()
|
||||
{
|
||||
stateMachine.Input.OnSprintEvent -= OnAirDash;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user