我让敌人沿着设定的路径(路径点)移动,但是我希望敌人在您距离玩家一定距离内时跟随玩家,但是当超出该距离时,敌人会回到其路径点并继续前进路径,直到玩家再次在一定距离内。所以它一直在这样做。下面是我得到的让敌人沿路点移动的代码。
我正在使用 Unity 2d 和 C#
[SerializeField]
Transform[] waypoints; //EnemyWayPoint;
[SerializeField]
float moveSpeed = 3f;
int waypointIndex = 0;
private void Start()
{
transform.position = waypoints[waypointIndex].transform.position;
}
private void Update()
{
Move();
}
private void Move()
{
transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
if (transform.position == waypoints[waypointIndex].transform.position) {
waypointIndex += 1;
}
if (waypointIndex == waypoints.Length)
waypointIndex = 0;
}
}
哈士奇WWW
慕的地6264312
相关分类