我目前正在尝试使一个对象移向另一个对象。到目前为止,我已经有这段代码可以尝试并实现它。
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句