Hi all,
I downloaded the following script from someone else for the purposes of creating the enemies in my videogame project, so that they would have at least basic behaviour. This script works fine up to a point, but once you move to the right of the Enemy, he ceases to search for you and instead just sits there, slowly inching towards the left side of the screen. The code (using A* pathfinding and its associated Seeker script) is here:
Eoin Moloney
using UnityEngine;
using Pathfinding; //Link between our script and the A* a.i. engine
using System.Collections;
[RequireComponent (typeof (Rigidbody2D))]
[RequireComponent (typeof (Seeker))]
public class EnemyAI : MonoBehaviour {
public Transform target;
public bool dead;
private bool bump;
//How many times per second we update our path
public float updateRate = 2f;
private Seeker seeker;
private Rigidbody2D rb;
//The calculated path
public Path path;
//The AI's speed per second (so that it's not framerate dependant)
public float speed = 300f;
public ForceMode2D fMode; //controls how force is applied to our RigidBody
[HideInInspector] //makes sure pathIsEnded does not show up in the Inspector despite beign a public variable
public bool pathIsEnded = false;
//the max distance from Waypoint A the enemy can be before it decides that it's arrived, and can move on to Waypoint B
public float nextWaypointDistance = 1;
//The waypoint we are currently moving towards
private int currentWaypoint = 0;
void Start () {
seeker = GetComponent();
rb = GetComponent();
if (target == null) {
Debug.LogError ("No player found. Panic!");
return;
}
seeker.StartPath (transform.position, target.position, OnPathComplete);
StartCoroutine (UpdatePath ());
}
IEnumerator UpdatePath()
{
if (target == null) {
}
seeker.StartPath (transform.position, target.position, OnPathComplete);
yield return new WaitForSeconds (1f / updateRate);
}
Does anyone have any idea why this might be the case? It's rather inconvenient to have the Enemy only work from the right-hand side.
Thanks.
↧