我目前正试图使一个对象移向另一个对象。到目前为止,我已经尝试并实现了这个代码。
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)
来实现这一点
我脑海中的一些伪代码。
Direction = Enemy.Position - Player.Position
Direction.Normalize()
Enemy.Position += Direction * Speed