Hello there!
I have a very annoying bug that occurs when I jump and start moving at the same time that makes my fps character move faster than it should.
Here is the code attached to the player without the unnecessary things :
void FixedUpdate () {
// Update the isRunning variable only if the player is grounded
if (controller.isGrounded) {
isRunning = inputManager.sprintInput;
}
// Execute all the scripts that move and rotate the player
ApplyGravity ();
RotateFromCamera ();
MoveFromInput ();
Jump ();
// Move
controller.Move (velocity * Time.fixedDeltaTime);
velocity = controller.velocity;
}
void ApplyGravity () {
// Check for exit cases
if (controller.isGrounded) {
velocity.y = -stickToGroundForce;
return;
}
// Change the velocity y
velocity.y -= stats.gravity;
}
void Jump () {
// Check if we are pressing the jump button
if (inputManager.jumpInput) {
// Set the jumpInput variable to false to prevent the player from jumping infinitly
inputManager.jumpInput = false;
// Check for exit cases
if (!controller.isGrounded) {
return;
}
// Set the velocity on the y axis to the jump force
velocity.y = stats.jumpForce;
}
}
void MoveFromInput () {
#region Get the speeds
// Control the speed on the x axis
targetHSpeed = (isRunning) ? stats.sprintSpeed * inputManager.moveInput.x : stats.runSpeed * inputManager.moveInput.x;
currentHSpeed = Mathf.SmoothDamp (currentHSpeed, targetHSpeed, ref hSpeedSmoothVelocity, GetInAirModifiedSmoothTime (speedSmoothTime));
// Control the speed on the z axis
targetVSpeed = (isRunning) ? stats.sprintSpeed * inputManager.moveInput.y : stats.runSpeed * inputManager.moveInput.y;
currentVSpeed = Mathf.SmoothDamp (currentVSpeed, targetVSpeed, ref vSpeedSmoothVelocity, GetInAirModifiedSmoothTime (speedSmoothTime));
#endregion
// Get the direction to move so we can't change it while jumping or falling
if (controller.isGrounded) {
right = transform.right;
forward = transform.forward;
} else {
right = (right != Vector3.zero) ? right : transform.right;
forward = (forward != Vector3.zero) ? forward : transform.forward;
}
// Update the velocity
velocity = currentHSpeed * right + Vector3.up * velocity.y + currentVSpeed * forward;
}
void RotateFromCamera () {
transform.eulerAngles = new Vector3 (transform.eulerAngles.x, mainCam.eulerAngles.y, transform.eulerAngles.z);
}
float GetInAirModifiedSmoothTime (float smoothTime) {
// Return the initial value if the player is grounded
if (controller.isGrounded) {
return smoothTime;
}
// Return the maximum value if the air control % = 0
if (airControlPercent == 0f) {
return float.MaxValue;
}
// Return the calculated smooth time if the exit cases are false
return smoothTime / airControlPercent;
}
The input manager updates the input variables on both update and fixed update calls and the camera rotates only on update calls