OK so with Impact State out the way, and the bugs giving me a bit of a break, I figured I’d continue implementing some states that existed in the third person, but are yet to be implemented in the course, to accelerate my personal process a bit
My next step was to debate between implementing a blocking state or a jumping state first… In the end, I went for a Jumping State first (since it’s the boring one). However, it’s not working as we speak, and I have no clue as of why. Here’s what I tried doing:
- Implement a ‘PlayerJumpingState.cs’ script from scratch:
using RPG.States.Player;
using UnityEngine;
public class PlayerJumpingState : PlayerBaseState
{
private readonly int JumpHash = Animator.StringToHash("Jump");
private Vector3 momentum;
public PlayerJumpingState(PlayerStateMachine stateMachine) : base(stateMachine) {}
public override void Enter()
{
stateMachine.ForceReceiver.Jump(stateMachine.JumpForce);
momentum = stateMachine.CharacterController.velocity;
momentum.y = 0;
stateMachine.Animator.CrossFadeInFixedTime(JumpHash, stateMachine.CrossFadeDuration);
}
public override void Tick(float deltaTime)
{
Move(momentum, deltaTime);
if (stateMachine.CharacterController.velocity.y <= 0)
{
stateMachine.SwitchState(new PlayerFallingState(stateMachine));
return;
}
FaceTarget(momentum, deltaTime);
}
public override void Exit() {}
}
- Implement a ‘PlayerFallingState.cs’, from scratch as well:
using RPG.States.Player;
using UnityEngine;
public class PlayerFallingState : PlayerBaseState
{
private readonly int FallHash = Animator.StringToHash("Fall");
private Vector3 momentum;
public PlayerFallingState(PlayerStateMachine stateMachine) : base(stateMachine) {}
public override void Enter()
{
momentum = stateMachine.CharacterController.velocity;
momentum.y = 0;
stateMachine.Animator.CrossFadeInFixedTime(FallHash, stateMachine.CrossFadeDuration);
}
public override void Tick(float deltaTime)
{
Move(momentum, deltaTime);
if (stateMachine.CharacterController.isGrounded) SetLocomotionState();
FaceTarget(momentum, deltaTime);
}
public override void Exit() {}
}
- Implement the ‘Jump’ function in ‘ForceReceiver.cs’:
public void Jump(float jumpForce)
{
verticalVelocity += jumpForce;
}
- Modify the Jumping state in ‘PlayerTestState.cs’ (what was the point of this state, apart from initial testing, again…?! Somehow it works though, so I assumed that’s where the Jump magic happens):
private void InputReader_HandleJumpEvent()
{
Debug.Log($"I get up, and nothing gets me down");
// stateMachine.SwitchState(new PlayerTestState(stateMachine));
stateMachine.SwitchState(new PlayerJumpingState(stateMachine));
}
- Handle the Jumping button in ‘InputReader.cs’:
public void OnJump(InputAction.CallbackContext context)
{
if (!context.performed) return;
JumpEvent?.Invoke();
}
- Name them properly in the Animation tab, so I named the Jump animation “Jump” (no tags) and the Falling Animation “Fall” (no tags either)
How do I move forward to get Jumping and Falling to work?