好吧,所以我做了一些ai警卫跟随我的角色。
我正在使用此代码:
private function getDegrees(radians:Number):Number
{
//return Math.floor(radians/(Math.PI/180));
return radians / 0.01745 | 0;
}
private function getRadians(delta_x:Number, delta_y:Number):Number
{
var r:Number = Math.atan2(delta_y, delta_x);
if (delta_y < 0)
{
//r += (2 * Math.PI);
r += 6.283;
}
return r;
}
然后在循环中
if(isShooting)
{
// calculate rotation based on mouse X & Y
_dx = this.x - _root.assassin.x;
_dy = this.y - _root.assassin.y;
// which way to rotate
_rotateTo = getDegrees(getRadians(_dx, _dy));
// keep rotation positive, between 0 and 360 degrees
if (_rotateTo > this.rotation + 180) _rotateTo -= 360;
if (_rotateTo < this.rotation - 180) _rotateTo += 360;
// ease rotation
_trueRotation = (_rotateTo - this.rotation) / _rotateSpeedMax;
// update rotation
this.rotation += _trueRotation;
gotoAndStop(5);
isWalking = false;
isStanding = false;
}
事情是,警卫奇怪地转动,好像他没有看着球员。就像玩家在其他地方一样。 Dunno,它只是不起作用..我不知道代码有什么问题!
答案 0 :(得分:0)
为了保持旋转为正,你应该使用模数360,如下所示,为了便于阅读和(代码)简单,除非它是一个明显的性能问题:(我认为这适用于AC3,只需在网上快速查看)
_rotateTo = _rotateTo % 360;
另一个可能的问题是如何定义_dx和_dy。我猜刺客正面临着与预期相反的方向。修复是使用我认为它应该是:
_dx = _root.assassin.x - this.x;
_dy = _root.assassin.y - this.y;
答案 1 :(得分:0)
你应该只计算相对角度
_rotateTo = getDegrees(getRadians(_dx, _dy));
_trueRotation = (_rotateTo - this.rotation) / _rotateSpeedMax;
然后将其标准化为尽可能接近零
_trueRotation = (_trueRotation+900) %360 - 180;
然后将其切割为每步的最大旋转
_trueRotation = max(_trueRotation,_rotateSpeedMax* _timestep);
_trueRotation = min(_trueRotation,-_rotateSpeedMax*_timestep);
然后使用它来更新前进方向
this.rotation += _trueRotation;
速度通常是每次更改,因此应该与语义一致性的某个时间步骤相乘。当然,你可能有时间步长等于1,或者rotateSpeedMax实际上是一个rotateMaxAngle(每步),然后可以删除乘以时间步长。