如何使对象在C#XNA中移向另一个对象
我目前正试图让一个物体向另一个物体移动。 到目前为止我有这个代码试图实现它。
double angleRad = Math.Atan2(mdlPosition.Z+treeTransform.Translation.Z, mdlPosition.X-treeTransform.Translation.X); enemyPos.X += (float)Math.Cos(angleRad) * 0.2f; enemyPos.Z += (float)Math.Sin(angleRad) * 0.2f;
每当我移动我的玩家角色时,对象会移动,但不会移动到角色的当前位置。 如何将其指向当前位置?
通常你应该这样做。 我想你希望敌人会向玩家移动。
Vector2 dir = player.Position - enemy.Position; dir.Normalize();
现在你只需要做每个周期:
enemy.Position += dir * speed;
编辑
为了使敌人面对玩家尝试计算dir
的角度并将其设置为绘制调用的rotation
参数。 你应该使用Math.Atan2(dir.Y, dir.X)
来实现这Math.Atan2(dir.Y, dir.X)
我的头在伪代码中的东西。
Direction = Enemy.Position - Player.Position Direction.Normalize() Enemy.Position += Direction * Speed