敌人用*(astar)&统一
如何让敌人停在距玩家一定距离处而不是径直走向玩家?我想创建一个远程单位。我可以攻击敌人,只是不想让敌人直接攻击玩家。我使用 AStar 寻路包对敌人进行了自定义 AI 脚本 -
Transform target;
public float speed = 200f;
public float nextWaypointDistance = 3f;
public Transform enemyGFX;
Path path;
int currentWaypoint = 0;
bool reachedEnd = false;
Seeker seeker;
Rigidbody2D rb;
void Start()
{
seeker = GetComponent<Seeker>();
rb = GetComponent<Rigidbody2D>();
target = GameObject.Find("Player").transform;
InvokeRepeating("UpdatePath", 0.0f, 0.5f);
}
void UpdatePath()
{
if(seeker.IsDone()) {
seeker.StartPath(rb.position, target.position, OnPathComplete);
}
}
void OnPathComplete(Path p)
{
if(!p.error)
{
path = p;
currentWaypoint = 0;
}
}
void FixedUpdate()
{
if (path == null) return;
if(currentWaypoint >= path.vectorPath.Count)
{
reachedEnd = true;
return;
}
else
{
reachedEnd = false;
}
Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
Vector2 force = direction * speed * Time.deltaTime;
rb.AddForce(force);
float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
if(distance < nextWaypointDistance) currentWaypoint++;
}
}
非常感谢所有帮助!
How do I make an enemy stop at a certain distance from the player rather than going right to it? I want to create a ranged unit. I can do the enemy attacks, I just don't want the enemy to go directly to the player. I have a custom AI script on my enemy using the AStar pathfinding package -
Transform target;
public float speed = 200f;
public float nextWaypointDistance = 3f;
public Transform enemyGFX;
Path path;
int currentWaypoint = 0;
bool reachedEnd = false;
Seeker seeker;
Rigidbody2D rb;
void Start()
{
seeker = GetComponent<Seeker>();
rb = GetComponent<Rigidbody2D>();
target = GameObject.Find("Player").transform;
InvokeRepeating("UpdatePath", 0.0f, 0.5f);
}
void UpdatePath()
{
if(seeker.IsDone()) {
seeker.StartPath(rb.position, target.position, OnPathComplete);
}
}
void OnPathComplete(Path p)
{
if(!p.error)
{
path = p;
currentWaypoint = 0;
}
}
void FixedUpdate()
{
if (path == null) return;
if(currentWaypoint >= path.vectorPath.Count)
{
reachedEnd = true;
return;
}
else
{
reachedEnd = false;
}
Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
Vector2 force = direction * speed * Time.deltaTime;
rb.AddForce(force);
float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
if(distance < nextWaypointDistance) currentWaypoint++;
}
}
All help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的方法是不搜索特定节点,而是搜索距离目标一定距离内的任何节点,或者一定距离内且具有直接视线的任何节点。
看起来 AStar 寻路库应该有类似的功能。例如,有一个 GetNearest 重载,它采用可能有效的约束。或者 maxNearestNodeDistance 字段。但我对那个库不熟悉,所以我很难提供具体的建议。
另一种选择是编写您自己的实现。这并非微不足道,但也不是过于复杂,并且有大量资源解释 A* 等算法。您可能无法达到与商业库同等的功能,但您可能只需要相当简单的功能。作为一种学习练习,它也可能很有用,可以更好地了解人工智能和路径查找的工作原理。
The correct way would be to not search for a specific node, but any node within some distance from the target, or any node within some distance and with a direct line of sight.
It looks like the AStar pathfinding library should have functions for things like that. There is for example a GetNearest overload that takes a constraint that might work. Or the
maxNearestNodeDistance
field. But I'm not familiar with that library, so I have difficulty providing specific suggestions.Another alternative would be to just write your own implementation. This is not trivial, but also not overly complex, and there are plenty of resources explaining algorithms like A*. You will probably not reach feature parity with a commercial library, but you might only need fairly simple functionality. It might also be useful as a learning exercise to get better knowledge on how AI and path finding works.
有点迟到答案,但也许可以帮助未来的人。
您可以执行IF目标距离是&lt; x ai.movespeed = 0,如果
值
到目标的距离再次返回AI.MovEspeed到其以前的
。
您必须加载iastarai ai = getComponent();首先,
这是艰难的。
Kinda late for an answer but maybe it helps future people.
You could do an if distance to target is <x ai.moveSpeed = 0 and if
the
distance to the target is >x again return ai.movespeed to its former
value.
You have to load IAstarAI ai = GetComponent(); in start for
that tough.