0

我正在用 c# xna 为我在大学的游戏设计课制作一个自上而下的 2D rpg 游戏。我正在尝试创建一个简单的 AI,将敌人移向玩家。目前我的代码如下

    /// <summary>
    /// method to move the enemy
    /// </summary>
    /// <param name="target">the position of the target</param>
    /// <returns>the new position to be moved to</returns>
    public virtual Vector2 move(Vector2 target)
    {
        Vector2 temp = (target - Position); // gets the difference between the target and position
        temp.Normalize();                   // sets the vector to unit vector
        temp *= moveSpeed;                  // sets the vector to be the length of moveSpeed
        float x = temp.X;
        float y = temp.Y;
        float xP, yP;
        double angle = Math.Acos(((x * direction.X) + (y * direction.Y)) / (temp.Length() * direction.Length())); //dot product finds the angle between temp and direction
        angle *= agility;                                                                                        //gets the angle to move based on agility
        xP = (float)(Math.Cos(angle) * (x - direction.X) - Math.Sin(angle) * (y - direction.Y) + x); 
        yP = (float)(Math.Sin(angle) * (x - direction.X) - Math.Cos(angle) * (y - direction.Y) + y);            // these lines rotate the point x,y around the direction vector by angle "angle"
        return new Vector2(xP, yP);
    }

目标在更新方法中正确传递:

    /// <summary>
    /// updates the enemy
    /// </summary>
    public void update()
    {
        this.Position = move(Game1.player.Position);
    }

但敌人根本不动。我在构造函数中添加了代码,确保敏捷性和移动速度不为 0。更改这些值没有任何作用。

谢谢你的帮助。

4

1 回答 1

0

在您的代码中,您返回此(方向角代码):

xP = (float)(Math.Cos(angle) * (x - direction.X) - Math.Sin(angle) * (y - direction.Y) + x);  
yP = (float)(Math.Sin(angle) * (x - direction.X) - Math.Cos(angle) * (y - direction.Y) + y);            // these lines rotate the point x,y around the direction vector by angle "angle" 

return new Vector2(xP, yP); 

但是您需要将其退回以进行移动:

temp *= moveSpeed;                  // sets the vector to be the length of moveSpeed    
float x = temp.X;    
float y = temp.Y; 
...

return new Vector2(x, y);
于 2012-10-03T19:11:36.367 回答