Hi,
I am using forces to control velocity for my player character in a 2D game.
As it is, whenever I jump, there is a brief jolt as my velocity is suddenly reduced, as if I was being held back.
I want to preserve my ground velocity going into the air, or at least make the transition between ground and air speed smoother.
The following function is where it all happens:
private void ApplyMovement()
{
if (isGrounded && !isOnSlope && !isJumping) //if not on slope
{
//Debug.Log("This one");
newVelocity.Set(movementSpeed * xInput, 0.0f);
speedDif = newVelocity.x - rb.velocity.x;
accelRate = (Mathf.Abs(newVelocity.x) > 0.01f) ? runAccel : runDeccel;
if (((rb.velocity.x > newVelocity.x && newVelocity.x > 0.01f) || (rb.velocity.x < newVelocity.x && newVelocity.x < -0.01f)))
{
accelRate = 0; //prevent any deceleration from happening, or in other words conserve are current momentum
}
if (Mathf.Abs(newVelocity.x) < 0.01f)
{
velPower = stopPower;
}
else if (Mathf.Abs(rb.velocity.x) > 0 && (Mathf.Sign(newVelocity.x) != Mathf.Sign(rb.velocity.x)))
{
velPower = turnPower;
}
else
{
velPower = accelPower;
}
float movement = Mathf.Pow(Mathf.Abs(speedDif) * accelRate, velPower) * Mathf.Sign(speedDif);
rb.AddForce(movement * Vector2.right);
//rb.velocity = newVelocity;
}
else if (isGrounded && isOnSlope && canWalkOnSlope && !isJumping) //If on slope
{
newVelocity.Set(movementSpeed * slopeNormalPerp.x * -xInput, movementSpeed * slopeNormalPerp.y * -xInput);
//speedDif = newVelocity.x - rb.velocity.x;
speedDif = newVelocity.x - rb.velocity.x;
accelRate = (Mathf.Abs(newVelocity.x) > 0.01f) ? runAccel : runDeccel;
if (((rb.velocity.x > newVelocity.x && newVelocity.x > 0.01f) || (rb.velocity.x < newVelocity.x && newVelocity.x < -0.01f)))
{
accelRate = 0; //prevent any deceleration from happening, or in other words conserve are current momentum
}
if (Mathf.Abs(newVelocity.x) < 0.01f)
{
velPower = stopPower;
}
else if (Mathf.Abs(rb.velocity.x) > 0 && (Mathf.Sign(newVelocity.x) != Mathf.Sign(rb.velocity.x)))
{
velPower = turnPower;
}
else
{
velPower = accelPower;
}
float movement = Mathf.Pow(Mathf.Abs(speedDif) * accelRate, velPower) * Mathf.Sign(speedDif);
rb.AddForce(movement * Vector2.right);
//rb.velocity = newVelocity;
}
else if (!isGrounded) //If in air
{
newVelocity.Set(movementSpeed * xInput, rb.velocity.y);
speedDif = newVelocity.x - rb.velocity.x;
accelRate = (Mathf.Abs(newVelocity.x) > 0.01f) ? runAccel * accelInAir : runDeccel * deccelInAir;
if (((rb.velocity.x > newVelocity.x && newVelocity.x > 0.01f) || (rb.velocity.x < newVelocity.x && newVelocity.x < -0.01f)))
{
accelRate = 0; //prevent any deceleration from happening, or in other words conserve are current momentum
}
if (Mathf.Abs(newVelocity.x) < 0.01f)
{
velPower = stopPower;
}
else if (Mathf.Abs(rb.velocity.x) > 0 && (Mathf.Sign(newVelocity.x) != Mathf.Sign(rb.velocity.x)))
{
velPower = turnPower;
}
else
{
velPower = accelPower;
}
float movement = Mathf.Pow(Mathf.Abs(speedDif) * accelRate, velPower) * Mathf.Sign(speedDif);
rb.AddForce(movement * Vector2.right);
//rb.velocity = newVelocity;
}
}