归航导弹,它们如何工作?
我想要创造什么
- 向玩家发射导弹的物体,如果与玩家发生碰撞,玩家将死亡。
问题
-
导弹是如何向玩家移动的。
-
如何使导弹移动,使其不会立即直接向玩家移动(以一个角度缓慢移动)。
我有一个让鼠标成为“玩家”和导弹的公式。
mouse = Mouse.GetState(); mousePosition = new Vector2(mouse.X, mouse.Y); A = (mouse.X - Position.X); B = (mouse.Y - Position.Y); C = (A * A) + (B * B); C = (float)Math.Sqrt(C); Angle1 = A / C; Angle2 = B / C;
(绘图是最好的!)
这只能获得从导弹到玩家的距离,现在我需要找到移动导弹的角度或东西,我需要做什么?
您可以获得从一个点到另一个点的距离,并将其转换为指向的方向。
//Find the delta time float delta = (float)gameTime.ElapsedGameTime.TotalSeconds * 60; //Find the direction Vector2 direction = mousePosition - misslePosition; direction.Normalize(); //Move towards it currentPos += direction * delta;
它需要乘以经过的时间,因此无论您运行的是什么FPS,它都显得相同。
您可能需要调整速度,但它应该创建一个这样的模式:
如果您希望导弹慢慢转向目标,您可以尝试使用MathHelper.Lerp
来缓慢改变角度。