I’m making a very simple 2d platforming game in 3d graphic style with unity. My main character is a character controller which moves in the direction of the Vector3 called moveDirection. I used the function Move from the character controller class to make him move.
being jumpSpeed a float number, and character, the refference to the character controller. So the thing works perfectly, it jumps as I wanted it to do, but when I compiled the game I found a problem. The character jumped less (significantly less, about a 10%) than in the test. I thought it would be a problem of frames per second, and I multpiplied the jumpSpeed by Time.deltaTime, but that hasn’t worked, because I’m giving an absolute number, not increasing it, so what it does now is jumping in a random value because of Time.deltaTime.
How can I fix this? I can change the jump speed but then it wouldn’t work in another computer with another FPS rate right? The other movement parts are done with Time.deltaTime and work well, but they’re not absolute numbers, they are an increase (or decrease) of the current value, so it works. I can’t do this with jumping because the result is not what I was expecting, so I need help on making this work.
The problem is the way you’re applying gravity: gravity must increase the down velocity proportionally to time. That’s why moveDirection.y is decreased by gravity * Time.deltaTime - it makes the gravity effect frame rate independent. Since you’re dividing gravity by 25, it will do the same effect at 25 fps, but will vary at other frame rates. Restore the Time.deltaTime factor like below:
function Update() {
...
// gravity * Time.deltaTime makes gravity fps independent
moveDirection.y -= gravity * Time.deltaTime;
monkey.Move(moveDirection*Time.deltaTime);
}
If you want a consistent jump height I would suggest moving your jump logic to fixed update and calculating the jump speed with Mathf.Sqrt(jumpHeight * -Physics.gravity.y)
Don’t forget that GetButtonDown won’t work reliably in FixedUpdate so keep the input check in Update but apply the jump in FixedUpdate