I've made my character script, and in it I set my FixedUpdate like so:
void FixedUpdate()
{
previousFrameVelocity = transform.position - previousFramePosition;
previousFramePosition = transform.position;
if (stateMachine != null)
{
isGrounded = controller.isGrounded;
stateMachine.MovementInput(movementInput); // Send input controls into my SM
Vector3 movement = stateMachine.FixedUpdate(); // SM outputs movement vector
currentVelocity = stateMachine.ProcessVelocity(currentVelocity); //applying gravity
Vector3 combined = movement + currentVelocity;
characterController.Move(combined * Time.fixedDeltaTime);
}
}
However, in the game, when I use previousFrameVelocity when my isGrounded state changes to see at which speed I impacted the ground, the results are infuriatingly inconsistent (and I've tried CharacterController's own .velocity, same thing, and a reason I went for explicitly calculating previousFrameVelocity in the first place), my logs show this after I moved the player exactly 10 meters above the ground several times (by simply editing the Transform inspector):
landing impact, velocity: 12,59808
landing impact, velocity: 2,384716
landing impact, velocity: 13,17809
landing impact, velocity: 3,737835
landing impact, velocity: 7,013631
landing impact, velocity: 4,580909
landing impact, velocity: 6,741642
By all accounts it shouldn't behave like this, since everything happens in FixedUpdate, It might be wrong numbers, but they should be consistently wrong, right? Not... this.
My code for calculating gravity is nothing complex either, it's basically velocity += Physics.gravity * Time.fixedDeltaTime