EnemyAI脚本不移动敌人



这是我第一次尝试AI,特别是跟随玩家的AI。我使用A*路径查找项目脚本,但使用Brackeys教程的代码https://www.youtube.com/watch?v=jvtFUfJ6CP8来源,以防需要。下面是代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
public class EnemyAI : MonoBehaviour
{
public Transform Target;
public float speed = 200f;
public float NextWayPointDistance = 3f;
Path path;
int currentWaypoint = 0;
bool ReachedEndOfpath = false;
Seeker seeker;
Rigidbody2D rb;
public Transform EnemyGraphics;
// Start is called before the first frame update
void Start()
{
seeker = GetComponent<Seeker>();
rb = GetComponent<Rigidbody2D>();
InvokeRepeating("UpdatePath", 0f, 1f);

}
void UpdatePath()
{
if (seeker.IsDone())
seeker.StartPath(rb.position, Target.position, OnPathComplete);
}
void OnPathComplete(Path p)
{
if (!p.error)
{
path = p;
currentWaypoint = 0;
}
}
// Update is called once per frame
void fixedUpdate()
{
if(path == null)
return;
if(currentWaypoint >= path.vectorPath.Count)
{
ReachedEndOfpath = true;
return;
}
else
{
ReachedEndOfpath = false;
}
Vector2 Direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
Vector2 Force = Direction * speed * Time.fixedDeltaTime;
rb.AddForce(Force);
float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
if(distance < NextWayPointDistance)
{
currentWaypoint++;
}

if(rb.velocity.x >= 0.01f)
{
EnemyGraphics.localScale = new Vector3(-1f, 1f, 1f);
}else if(rb.velocity.x <= 0.01f)
{
EnemyGraphics.localScale = new Vector3(1f, 1f, 1f);
}
}
}

如何解决这个问题:

  1. 我认为这可能是速度的问题,所以我把它增加到10000000,仍然没有
  2. 接下来我认为这是Rigidbody2d的问题,所以我检查那里,发现重力尺度设置为0,所以我把它增加到1。它使我的敌人倒在地上,但仍然没有移动。
  3. 我认为这可能是质量和拖动的问题,所以我将线性拖动和角度拖动设置为0,并将质量设置为1。还没有。
  4. 我将车身类型设置为运动学,按下运行,没有。设置主体类型为静态,按下运行,无。设置主体类型为动态,按下运行,仍然没有。
  5. 我试着创造一个新的目标让敌人跟随,将空的游戏对象I拖入目标,按下run,仍然没有移动。

我不知道如何解决这个问题。

请帮忙吗?

看起来像是一个打字错误?你有:

// Update is called once per frame
void fixedUpdate()
{

,但该方法称为FixedUpdate(),前面有一个大的FfixedUpdateNOT一样的。

最新更新