Hello! I’ve been following along with ETeeski’s tutorials here, and I’ve gotten stuck. My enemies can follow me perfectly inside of the requisite cells, but once I move or jump out of the cells, they stop following me. The current cell and goalDoor are always set except when I’m not in a cell, so I can’t for the life of me figure out the problem!
EnemyMovementScript.js
var currentCell : GameObject;
var playerMovementScript : PlayerMovementScript;
var playerTransform : Transform;
var playerCell : GameObject;
var goalDoor : GameObject;
var shortestPathSoFar : float;
@HideInInspector
var waitToStart : int = 5;
var currentMoveSpeed : float = 5;
var randomizedCourse : boolean = false;
var randomizeCourseVector : Vector3;
var calculatedNewRandomizeCourseVector : boolean;
function Awake ()
{
shortestPathSoFar = Mathf.Infinity;
playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
playerTransform = GameObject.FindWithTag("Player").transform;
waitToStart = 5;
randomizeCourseVector = transform.position;
}
function Update ()
{
if (waitToStart <= 0)
{
playerCell = playerMovementScript.currentCell;
for (var doorCheckingNow : GameObject in currentCell.GetComponent(AIPathCellScript).doors)
{
for (var i : int = 0; i <= doorCheckingNow.GetComponent(AIPathDoorScript).cells.length - 1; i++)
{
if (doorCheckingNow.GetComponent(AIPathDoorScript).cells[i] == playerCell)
if (doorCheckingNow.GetComponent(AIPathDoorScript).doorsToCells[i] < shortestPathSoFar)
{
goalDoor = doorCheckingNow;
shortestPathSoFar = doorCheckingNow.GetComponent(AIPathDoorScript).doorsToCells[i];
}
}
}
shortestPathSoFar = Mathf.Infinity;
}
waitToStart -= 1;
if (!calculatedNewRandomizeCourseVector)
{
randomizeCourseVector = FindRandomSpotInCurrentCell();
calculatedNewRandomizeCourseVector = true;
}
if (goalDoor)
if (!goalDoor.GetComponent(AIPathDoorScript).doorOpen)
goalDoor = null;
//If not in same cell, move towards goalDoor
if (currentCell != playerCell || playerCell == null)
{
if (randomizedCourse)
transform.position += (goalDoor.transform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
if (!randomizedCourse)
{
transform.position += (randomizeCourseVector - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, randomizeCourseVector) < transform.localScale.x)
{
if (goalDoor)
randomizedCourse = true;
if (goalDoor == null)
calculatedNewRandomizeCourseVector = false;
}
}
}
//If in same cell, move towards player
if (playerCell == currentCell)
transform.position += (playerTransform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
}
function OnTriggerEnter (hitTrigger : Collider)
{
if (hitTrigger.tag == "AIPathCell")
{
currentCell = hitTrigger.gameObject;
randomizedCourse = false;
calculatedNewRandomizeCourseVector = false;
}
}
//
function FindRandomSpotInCurrentCell ()
{
return currentCell.transform.position + (currentCell.transform.rotation * Vector3(Random.Range(currentCell.transform.localScale.x * -0.5,currentCell.transform.localScale.x * 0.5),0,Random.Range(currentCell.transform.localScale.z * -0.5,currentCell.transform.localScale.z * 0.5)));
}