我正在开发一个 2.5D 游戏(想想自上而下的地牢爬行者/侧视图,如以撒的结合),其中角色可以在所有八个方向上行走。播放器动画工作正常,但 AI 动画在运动状态之间闪烁不少。AI 根据其方向移动并选择动画:
//Select which animation we should currently be in:
if (velocity != Vector2.Zero)
{
if (direction.X < 0 && direction.Y < 0 && currentAnimation.name != "Walk Up Left")
ChangeAnimation(Animations.WalkUpLeft);
else if (direction.X > 0 && direction.Y < 0 && currentAnimation.name != "Walk Up Right")
ChangeAnimation(Animations.WalkUpRight);
else if (direction.X < 0 && direction.Y > 0 && currentAnimation.name != "Walk Down Left")
ChangeAnimation(Animations.WalkDownLeft);
else if (direction.X > 0 && direction.Y > 0 && currentAnimation.name != "Walk Down Right")
ChangeAnimation(Animations.WalkDownRight);
else if (direction.X < 0 && direction.Y == 0 && currentAnimation.name != "Walk Left")
ChangeAnimation(Animations.WalkLeft);
else if (direction.X > 0 && direction.Y == 0 && currentAnimation.name != "Walk Right")
ChangeAnimation(Animations.WalkRight);
else if (direction.Y > 0 && direction.X == 0 && currentAnimation.name != "Walk Down")
ChangeAnimation(Animations.WalkDown);
else if (direction.Y < 0 && direction.X == 0 && currentAnimation.name != "Walk Up")
ChangeAnimation(Animations.WalkUp);
}
else if (velocity == Vector2.Zero)
{
if (currentAnimation.name != "Idle")
ChangeAnimation(Animations.Idle);
}
问题是有时 AI 必须沿对角线移动,在这样做之后,他会直线向下移动几帧以纠正自己,因此他完全在网格上……这只是几帧,所以在视觉上看起来在两个方向之间进行非常故障。
方向变量永远不会有浮点值... AI运动代码是这样的:
计算我们需要移动到的下一个图块。
让我们的方向变量看这个图块。
舍入,因此方向始终为整数,以避免浮点数的抖动。
我用来四舍五入的代码是这样的:
private void TrimDirection()
{
//Snap our direction to one of the eight cardinal directions:
direction.X = (float)Math.Round(MathHelper.PiOver4 * (float)Math.Round(direction.X / MathHelper.PiOver4));
direction.Y = (float)Math.Round(MathHelper.PiOver4 * (float)Math.Round(direction.Y / MathHelper.PiOver4));
}
运动代码是这样的:
if (direction.X > 0)
MoveRight();
else if (direction.X < 0)
MoveLeft();
if (direction.Y > 0)
MoveDown();
else if (direction.Y < 0)
MoveUp();
我一直在试图弄清楚如何解决这个问题并且我碰壁了......我是否需要实现某种类型的动画滞后以避免动画变化如此之快?还是这个问题在我如何让 AI 移动的更深层次?
提前致谢!