Framerate Independence for 2D Platformer


This is my first post about my 2D platformer character jumping higher when the frame rate drops.

Here are some code snippets as I am unsure where else I need to do a Time.deltaTime multiplier. I would appreciate any feedback on this.

void UpdateMovementOffsets()
		isMovementBlocked = false;
		// Check to see if we can move horizontally in this direction
		if(playerBlockedLeft && currentDirection == Vector3.left) isMovementBlocked = true;
		if(playerBlockedRight && currentDirection == Vector3.right) isMovementBlocked = true;
		// Make sure we are able to move in the horizontal direction we are attempting to move by checking if we are blocked
			horizontalMovementOffset = currentDirection * horizontalSpeed * Time.deltaTime;
			horizontalMovementOffset = currentDirection * 0.0f; //no movement
		// Calculate Verital movement offset for player for this frame
			verticalMovementOffset = new Vector3(0.0f, verticalSpeed, 0.0f) * Time.deltaTime;
			verticalMovementOffset = new Vector3(0.0f, 0.0f, 0.0f);	

Then the two functions I actually calculate the vertical and horizontal offsets.

void UpdateHorizontalMovement()
		targetSpeed = Mathf.Min(Mathf.Abs(horizontalMoveValue), 1.0f); //returns the smallest of two or more values.
		targetSpeed *= walkSpeed;
		horizontalSpeed = Mathf.Lerp(horizontalSpeed, targetSpeed, speedSmoothing); //interpolates between a and b by t. t is clamped between 0 

…and for vertical movement

void UpdateVerticalMovement()
		// Adjust for gravity
				verticalSpeed -= gravity;
		// Make sure Player does not fall too fast
		if(verticalSpeed < -maxFallSpeed)
			verticalSpeed = -maxFallSpeed;
		// Handle a player jump action
			playerIsOnTheGround = false;
			verticalSpeed = Mathf.Sqrt(50 * jumpHeight * gravity);
			AudioSource.PlayClipAtPoint(jumpSound, myTransform.position, 0.75f); //last value is for volume

I’m pretty sure your character jumps higher with low framerate because you’re subtracting gravity from the speed in a frame rate independent fashion- if you think about it, at a high frame rate, you’re speed is going to decrease much faster than at a low frame rate. you need to multiple gravity by Time.deltaTime as well ( and then scale it appropriately so that at your target framerate it’s still the number you expect ). In other words, you acceleration needs to be based on deltaTime as well as your Velocity. Good luck!

I think I get what you are saying and it probably should read " you’re NOT subtracting gravity from the speed in a frame rate independent fashion".

I will try and apply the Time.deltaTime to all the values I have specified for calculations (gravity, maxFallSpeed, jumpHeight, walkSpeed and speedSmoothing) as well as scaling factors and try and dial the movement in again to work like it should.

Yes, I am using Update(). I have solved the horizontal framerate independent movement but the jumping has been stubborn.

Ok, I solved the problem through trial and error. My 2D platformer is now be framerate independent. Thanks for the feedback. I will post my solution in a few days.