Reputation: 513
Basicly, I've tried to code velocity into my game, which works sort of. The deaccelaration works, but not just right.
My problem is, that theres no way for my velocity z and x, to reach 0, because it just fights between both positive and negative values, according to my code also.
But I can't see any other way, to stop this from happening, and decreasing correctly to 0.
My code:
if((!cc.isGrounded)) {
velocity.y -= gravity;
}
if((velocity.z != 0)) {
velocity.z = Mathf.Lerp(velocity.z, 0, deaccelerationSpeed);
}
if((velocity.x != 0)) {
velocity.x = Mathf.Lerp(velocity.x, 0, deaccelerationSpeed);
}
if(isRightDown) {
velocity.x = sidewayMovementSpeed;
} else if(isLeftDown) {
velocity.x = -sidewayMovementSpeed;
}
if(isForwardDown) {
velocity.z = forwardMovementSpeed;
} else if(isBackDown) {
velocity.z = -backwardMovementSpeed;
}
What I'm pretty much is asking, is there any different way to handle velocity, or is there a fix to my problem?
Upvotes: 0
Views: 670
Reputation: 4545
You could implement some logic that will treat small values of velocity as 0, so that it wouldn't oscillate between positive and negative values.
EDIT:
Something like this
if(Math.Abs(velocity.x) > TOLERATED_VELOCITY)
{
velocity.x = Mathf.Lerp(velocity.x, 0, deaccelerationSpeed);
}
else
{
velocity.x = 0;
}
where TOLERATED_VELOCITY
is a constant specifying which values of velocity to consider zero.
Upvotes: 2