YO!
Here’s a link to the video giving me the problem:
Followed Eteeski’s AI tutorials (very helpful BTW), but encountered a problem.
When the game starts, the enemy immediatly leaves his cell and wanders off to a random part of the map in a straight line. He then heads back towards his previous cell, and then back to the random point BEFORE re-entering the cell.
I’ve tried increasing the size of the cells, i added the piece of code to the player movement script which renders the currentCell null on the player exiting his cell.
I get no errors.
If you wish to see the other scripts please let me know, but all was working fine until i made the 1.36 adjustments to the script.
I’ll put the EnemyMovementScript code below (and i’m afraid it comes with my amazing notes):
var currentCell : GameObject;
var playerMovementScript : PlayerMovementScript;
var playerTransform : Transform;
var playerCell : GameObject;
var goalDoor : GameObject;
var shortestPathSoFar : float;
var waitToStart : float = 5;
var currentMoveSpeed : float = 2;
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;
}
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;
}
//moves the 'enemy' toward the target door
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;
}
}
}
//moves the 'enemy' toward the '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;
calculateNewRandomizeCourseVector = false;
}
}
//The following function may not be nessercery...*seethe*
/*function OnTriggerStay (hitTrigger : Collider)
{
if (hitTrigger.tag == "AIpathCell")
{
currentCell = hitTrigger.gameObject;
}
}*/
//when function is called, 'enemy' will go to a random spot in it's cell before moving to the next door
function FindRandomSpotInCurrentCell ()
{
return currentCell.transform.position + (currentCell.transform.rotation * Vector3
( /* X */ Random.Range(currentCell.transform.localScale.x * -05, currentCell.transform.localScale.x * 05),
/* Y */ 0,
/* Z */ Random.Range(currentCell.transform.localScale.z * -05, currentCell.transform.localScale.z * 05)));
}
If you wish to see the other scripts please let me know, but all was working fine until i made the 1.36 adjustments to the script.