Hello, my team and I have it a wall in regards to our code that governs the patrol and hunting of our Enemy AI controlled objects in our game. The Code, in terms of hunting and chasing, works perfectly, The issue lies in the patrol portion itself. The Object for some reason always wants to move to a certain location on the Z axis, as far as I can tell, I am not defining this. Any suggestions would be greatly appreciated, this is just a snippet of the file, if requested I can post the whole thing, but this is where the patrol issue is occurring.
else if(distance > 20.0)//Patrol Range
{
Debug.LogWarning("Lost Target, returning to patrol");
//Look for Target, Where are the savings... Savings!
patrolAmount.x = patrolInc * ((float)moveSpeed * 2f) * Time.deltaTime;
if (patrolInc > 0.0f && transform.position.x >= patrolRangePos)
{
patrolInc = -1.0f;
}
else if (patrolInc < 0.0f && transform.position.x <= patrolRangeNeg)
{
patrolInc = 1.0f;
}
transform.Translate(patrolAmount);
}
Here is the full Code as requested:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
public Transform target;//This is what the AI looks for, the Target must be tagged "Player"
public int moveSpeed;// This is a base value of how fast the AI moves
public int rotationSpeed;// This is a Base Value of how fast the AI rotates
public int powerWalkMultiplier = 2;//The Multiplier for Power Walking
public int runSpeedMultiplier = 4;//The Multiplier for Running
public float patrolRangePos = 0.0f;//How far does the AI patrol
public float patrolRangeNeg = 0.0f;//^
private float patrolInc = 0.25f;// increment of the Patrol
private Vector3 patrolAmount;
private Transform myTransform;
void Awake()
{
myTransform = transform;//Keeps it Active
}
// Use this for initialization
void Start ()
{
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
// Update is called once per frame
void Update ()
{
Debug.DrawLine(target.position, myTransform.position, Color.green);//Plots course
float distance = Vector3.Distance (this.transform.position, GameObject.FindGameObjectWithTag("Player").transform.position);//Calculate Distance
if(distance < 10.0)//Chase Range
{
//Debug.LogWarning("Enemy Sighted! Pursuing!!!");
Debug.DrawLine(target.position, myTransform.position, Color.red);
//Look At Target, Oh look! Target!
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//This takes the Cube's transform via a Slerp, it's a rotation based on the angle it's loking at
//Move to Target, Hooray Savings!
myTransform.position += myTransform.forward * (moveSpeed * runSpeedMultiplier) * Time.deltaTime;//Trigger Run Speed
}//End Chase Range
else if(distance < 20.0)//Investigate Range
{
//Debug.LogWarning("Enemy Detected, Investigating...");
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look At Target, Oh look! Target!
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//This takes the Cube's transform via a Slerp, it's a rotation based on the angle it's loking at
//Move to Target, Hooray Savings!
myTransform.position += myTransform.forward * (moveSpeed * powerWalkMultiplier) * Time.deltaTime;//Trigger power walk Speed
}//End Investigate Range
else if(distance > 20.0)//Patrol Range
{
//Debug.LogWarning("Lost Target, returning to patrol");
//Look for Target, Where are the savings... Savings!
patrolAmount.x = patrolInc * ((float)moveSpeed * 2f) * Time.deltaTime;
if (patrolInc > 0.0f && transform.position.x >= patrolRangePos)
{
patrolInc = -1.0f;
}
else if (patrolInc < 0.0f && transform.position.x <= patrolRangeNeg)
{
patrolInc = 1.0f;
}
transform.Translate(patrolAmount);
}
}
}