0

基本上,我已经尝试将速度编码到我的游戏中,这很有效。减速有效,但并非恰到好处。

我的问题是,根据我的代码,我的速度 z 和 x 无法达到 0,因为它只是在正值和负值之间进行斗争。

但我看不到任何其他方式来阻止这种情况的发生,并正确地减少到 0。

我的代码:

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;
}

我几乎要问的是,是否有任何不同的方式来处理速度,或者是否可以解决我的问题?

4

1 回答 1

2

您可以实现一些将小速度值视为 0 的逻辑,这样它就不会在正值和负值之间振荡。

编辑:

像这样的东西

if(Math.Abs(velocity.x) > TOLERATED_VELOCITY)
{
    velocity.x = Mathf.Lerp(velocity.x, 0, deaccelerationSpeed);
}
else
{
    velocity.x = 0;
}

其中TOLERATED_VELOCITY是一个常数,指定将哪些速度值视为零。

于 2013-09-06T09:57:53.413 回答