I’m trying to implement simple jump physics to my player with a character controller and using the unity Physics, but my jump height is slightly inconsistent but all my parameters that interract with the jump are at all times except for the FPS which then assume to be the issue. I can’t really show it clearly cause no videos, but basically that seems to be the issue. My fps limiter is set to 144 but the fps varies wildly.
FPS1
FPS2
FPS3
And the Jump height itself as well.
JumpHeight1
JumpHeight2
JumpHeight3
I tried adding or deleting the FPS limiter, changing the targetFPS, I tried touching to the jump params, I tried changing methods in how I manage the jump, before trying the Physics.gravity method I actually had my own gravity with a velocity calculation that looked like this.
velocity += jumpforce * 3f * gravity
when jumping
velocity -= gravity * gravityScaling
when airborne
movement.y = velocity * Time.deltaTime
at the moment the movement is applied.
I tried to use FixedUpdate instead of Update, or fixedDeltaTime instead of deltaTime… I’m lost. I have no idea how to fix the issue because I don’t know how to touch to how the updates are done based on the FPS more than I already have, and even if I think that’s the only possible issue I see, I’m not even sure the Issue comes from the FPS for sure.
Here is the code of the player when airborne.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAirborne : MonoBehaviour
{
public void UpdateAirborne(Player player)
{
AddMovementWalking(player);
Accelerate(player);
Move(player);
}
void Move(Player player)
{
player.movement.x *= player.speed * Time.deltaTime;
player.movement += Physics.gravity / 10 * Time.deltaTime;
player.characterController.Move(player.movement);
}
void Accelerate(Player player)
{
if (player.playerStates.HasFlag(Player.States.Walking))
{
if (player.horizontalJoystickPush * player.movement.x < 0)
{
player.speed -= player.acceleration / 2 * Time.deltaTime;
player.momentumSpeed -= player.acceleration / 2 * Time.deltaTime;
if (player.speed > player.runningSpeed)
{
player.speed = player.runningSpeed;
}
}
else
{
if (player.horizontalJoystickPush != 0)
{
player.speed += player.acceleration * Time.deltaTime;
if (player.momentumSpeed < player.runningSpeed)
{
player.momentumSpeed += player.acceleration * Time.deltaTime;
player.momentumDir = player.movement.x;
}
}
if (player.runningSpeed < player.momentumSpeed)
{
if (player.speed > player.momentumSpeed)
{
player.speed = player.momentumSpeed;
}
}
else
{
if (player.speed > player.runningSpeed)
{
player.speed = player.runningSpeed;
}
}
}
if (player.speed < 0)
{
player.speed = 0;
}
}
}
void AddMovementWalking(Player player)
{
if (player.speed <= player.momentumSpeed)
{
player.movement.x = player.momentumDir;
}
else
{
player.movement.x = player.horizontalJoystickPush;
}
}
}
Here is the code of the player when grounded, and the jump function too.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerGrounded : MonoBehaviour
{
public void UpdateGrounded(Player player)
{
AddMovementWalking(player);
Accelerate(player);
Move(player);
Jump(player);
}
void Accelerate(Player player)
{
float signAccel = -1;
if (player.playerStates.HasFlag(Player.States.Walking))
{
signAccel = 1;
}
player.speed += signAccel * player.acceleration * Time.deltaTime;
if (player.speed < 0f)
{
player.speed = 0f;
}
if (Mathf.Abs(player.horizontalJoystickPush) < 0.5f)
{
if (player.speed > player.walkingSpeed)
{
player.speed = player.walkingSpeed;
}
}
else
{
if (player.speed > player.runningSpeed)
{
player.speed = player.runningSpeed;
}
}
}
void AddMovementWalking(Player player)
{
if (player.playerStates.HasFlag(Player.States.Walking))
{
if (!InputsManager.Instance.controller)
{
if (player.playerStates.HasFlag(Player.States.FacingRight))
{
player.horizontalJoystickPush = 1;
}
else
{
player.horizontalJoystickPush = -1;
}
}
}
player.movement.x = player.horizontalJoystickPush;
}
void Move(Player player)
{
player.momentumSpeed = player.speed;
player.momentumDir = player.movement.x;
player.movement.x *= player.speed * Time.deltaTime;
player.movement.y = -1;
player.characterController.Move(player.movement);
}
void Jump(Player player)
{
if (player.playerStates.HasFlag(Player.States.CanJump))
{
player.movement.y = player.jumpForce;
player.playerStates |= Player.States.AirBorne;
player.playerStates &= ~Player.States.CanJump;
}
}
}
And here is the player object: PlayerObject1 PlayerObject2