Hey all, I have a platformer asset that I really like, but its having a weird bug. I contacted the asset creator but they live half way across the world and was hoping to get some advice from the lovely people here. The problem I am having is in the jump function. It seems that at times my player will randomly jump way too high, possibly when the game lags a bit. I have things set up the way I like, then the game will slow a little because of lag and then my player will jump over twice as high as before. The weird part is that it doesn’t always happen when the game lags, and often when the game stops lagging I still jump way too high. So I have no idea what is going on with it. The code is a bit confusing, so I’m not sure where the problem is.
Here is the code that has to do with jumping:
private void applyJump()
{
if (_isGrounded || _body.velocity.y < 0)
_isEdgeJumping = false;
if (!_wantsToJump)
return;
if (_isFalling)
return;
if ((!_isGrounded && !_canDoSecondJump) && !IsHangingOnEdge && !IsHangingOnRope && !IsOnWall)
return;
if (_jumpTimer > float.Epsilon)
return;
_isDashing = false;
_postDashGravityTimer = 0;
if (_isDoingCombo && _wantsToJump)
_wantsToBreakCombo = true;
_jumpTimer = 0.25f;
var v = _body.velocity;
v.y = _isGrounded ? Air.GroundJumpStrength : Air.AirJumpStrength;
if (IsOnWall)
{
v += _wallNormal * Wall.JumpStrength;
_wallJumpTimer = Wall.JumpTime;
if (Vector3.Dot(_wallNormal, transform.forward) < 0)
{
if (Direction == CharacterDirection.Right)
Direction = CharacterDirection.Left;
else
Direction = CharacterDirection.Right;
}
}
else if (IsHangingOnEdge)
{
if (!_isEdgeClimbing)
_isEdgeJumping = true;
_isEdgeClimbing = false;
}
else if (IsHangingOnRope)
{
var r = _handGrabbedRope.Object.Velocity;
if (r.magnitude > Climbing.RopeJumpStrength)
v += r.normalized * Climbing.RopeJumpStrength;
else
v += r;
_freeTimedRope = _handGrabbedRope.Object.Root;
_handGrabbedRope = new RopeBit();
}
else if (!_isGrounded && _canDoSecondJump)
{
_isSecondJumping = true;
_secondJumpTimer = 0.4f;
_canDoSecondJump = false;
}
_body.velocity = v;
if (_edge != null)
Release(_edge);
_wantsToJump = false;
_isJumping = true;
}
and there’s a bit of code that has to do with gravity, which might also be the issue but I’m not sure.
Code:
private void applyGravity(float dt)
{
if (!IsAlive)
{
_body.velocity -= new Vector3(0, Air.Gravity, 0) * dt;
return;
}
var strength = Air.Gravity;
if (IsOnWall && _body.velocity.y < 0)
strength = Wall.Gravity;
var force = new Vector3(0, strength, 0) * dt;
if (IsHangingOnEdge)
{
if (_isEdgeClimbing)
_body.velocity = Vector3.zero;
else
{
_isEdgeJumping = false;
var velocity = _body.velocity - force;
var miny = _edge.Top + Climbing.EdgeHeight - height;
if (transform.position.y < miny + 0.1f)
velocity = Vector3.Lerp(velocity, Vector3.zero, Time.deltaTime * 30);
if (transform.position.y < miny)
transform.position = new Vector3(transform.position.x, miny, transform.position.z);
_body.velocity = velocity;
}
}
else if (IsHangingOnRope)
{
}
else if (!_isGrounded && !_isDashing && _postDashGravityTimer > 0)
{
}
else if (_noMovementTimer < 0.1f || !_isGrounded || IsOnSlope || _groundTimer < 0.2f)
{
if (_isGrounded && _jumpTimer < 0.1f)
{
if (_isDashing)
_body.velocity -= force * 10;
else
_body.velocity -= force * 2;
}
else
_body.velocity -= force;
}
}
Any help at all would be amazing for this!