I am developing an endless runner mobile game, Similar to Subway Surfers. I make my player character rotate slightly left or right and then return to facing forward when sliding left or right. The issue is that this rotation makes the character's jump inconsistent, resulting in different jump heights.
How can I make the jump consistent? Below are the scripts I am using.
In the PlayerController script:
public void SetVelocity(Vector3 velocity)
{
CController.Move(velocity * Time.deltaTime);
//Rotate the player a bit where is going
Vector3 dir = CController.velocity;
if (dir != Vector3.zero)
{
dir.y = 0;
transform.forward = Vector3.Lerp(transform.forward, dir, data.RotationSpeed * Time.deltaTime);
}
}
In PlayerJumping script:
public override void Enter()
{
player.velocityY = data.jumpForce;
}
private void UpdateJumpMovement()
{
// Aplly gravity
player.velocityY += data.gravity * Time.deltaTime;
Vector3 pos = Vector3.zero;
pos.x = player.SnapToLane();
pos.y = player.velocityY;
pos.z = player.currentSpeed;
player.SetVelocity(pos);
}
jumpForcedirectly tovelocityY, not applying it as a force. So I'd recommend naming that variablejumpSpeedinstead, to communicate it's a speed measured in metres per second, not a force measured in Newtons. Similarly,posis being passed intoSetVelocity(), so it's really avelocityorvel, not a "position" as its current naming implies. Renaming the variables won't solve this problem, but it makes the code easier to reason about and makes it less tempting to introduce errors down the line with false assumptions. \$\endgroup\$CControllerin the first script? Is that a reference to the standard CharacterController provided by Unity or something else? \$\endgroup\$datastructure with fieldsjumpForceandgravity? What is the component thatplayerreferences? \$\endgroup\$