Character sometimes moves faster when I jump and start moving at the same time
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
Alright, so I am pretty sure this is appening because of the GetInAir$$anonymous$$odifiedSmoothTime () function: the current speed takes too much time to go back to zero and the jump is waaaaay too long. So right now I am looking for a solution; maybe I can just affect the input when I am not grounded ins$$anonymous$$d of affecting the smoothTime.
Answer by Had1t1x · Jan 20, 2018 at 06:46 PM
Yay! Bug fixed! So now I am affecting the input, as I said before. Basically I have a new lastGroundedInput variable that is equals to the inputHandler.lastMoveInput (equals to the moveInput recorded at each start of Update () calls) and that stays the same while not grounded. Now I have this function :
Vector2 GetInAirModifiedMoveInput (Vector2 input, Vector2 lastGroundedInput) {
// Return the initial value if the player is grounded
if (isGrounded) {
return input;
}
// Return the calculated input if we are grounded
return lastGroundedInput * (1f - airControlPercent) + input * airControlPercent;
}
You can see it like this: if the airControlPercent is equals to 90% (.9f), then you will have 10% (.1f) of the input that will stay the same during the jump/fall (so lastGroundedInput x ( 1f - airControlPercent)) and 90% of the input that will be equals to the current recorded input (input x airControlPercent).
Your answer
Follow this Question
Related Questions
Standard FPS Controller - Sounds Cancelling One Another 0 Answers
How Do I Get My Player To Move With The Block That Its On 1 Answer
Control Air movement with character controller 0 Answers
VR character rotation not follow the camera 1 Answer
Field ' ' is never assigned to and will always have its default value 0. Pls help me 0 Answers