So, I have tried translating Renaissance Coders AI Script for C# to Js, It shows no error’s but does not work neither. The enemy proceeds to waypoint’s and but does not stop for investigation or chase while in Chase State.
The Script:
#pragma strict
public var agent : NavMeshAgent;
public var PlayerTagName : String;
public enum AIState {
PATROL,
CHASE,
INVESTIGATE
}
public var state : AIState;
private var alive : boolean;
// PATROL Vars
@Header ("GameObject's with 'Waypoint' tag will be added automatically")
public var waypoints : GameObject[];
private var waypointInd : int;
public var patrolSpeed : float = 0.5f;
// CHASE Vars
public var chaseSpeed : float = 1f;
public var target : GameObject;
// IINVESTIGATE Vars
private var investigateSpot : Vector3;
private var timer : float = 0;
public var investigateWait : float = 10;
//Sight Vars
public var heightMultiplier : float;
public var sightDist : float = 10;
// Init
function Start() {
agent = GetComponent.<NavMeshAgent>();
agent.updatePosition = true;
agent.updateRotation = false;
waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
waypointInd = Random.Range (0, waypoints.Length);
var state = AIState.PATROL;
alive = true;
heightMultiplier = 1.36f;
StartCoroutine("FSM");
}
function FSM(){
while (alive){
switch(state)
{
case AIState.PATROL:
Patrol();
break;
case AIState.CHASE:
Chase();
break;
case AIState.INVESTIGATE:
Investigate();
break;
}
return;
}
}
function Patrol()
{
agent.speed = patrolSpeed;
if(Vector3.Distance (this.transform.position, waypoints[waypointInd].transform.position) >= 2)
{
agent.SetDestination(waypoints[waypointInd].transform.position);
}
else if(Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) <= 2)
{
waypointInd = Random.Range(0, waypoints.Length);
}
else
{
agent.SetDestination(Vector3.zero);
}
}
function Chase()
{
agent.speed = chaseSpeed;
agent.SetDestination(target.transform.position);
}
function Investigate()
{
timer += Time.deltaTime;
var hit : RaycastHit;
Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, transform.forward * sightDist, Color.green);
Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, (transform.forward + transform.right).normalized * sightDist, Color.green);
Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, (transform.forward - transform.right).normalized * sightDist, Color.green);
if(Physics.Raycast (transform.position + Vector3.up * heightMultiplier, transform.forward, hit, sightDist))
{
if(hit.collider.gameObject.tag == PlayerTagName)
{
state = AIState.CHASE;
target = hit.collider.gameObject;
}
}
if(Physics.Raycast (transform.position + Vector3.up * heightMultiplier, (transform.forward + transform.right).normalized, hit, sightDist))
{
if(hit.collider.gameObject.tag == PlayerTagName)
{
state = AIState.CHASE;
target = hit.collider.gameObject;
}
}
if(Physics.Raycast (transform.position + Vector3.up * heightMultiplier, (transform.forward - transform.right).normalized, hit, sightDist))
{
if(hit.collider.gameObject.tag == PlayerTagName)
{
state = AIState.CHASE;
target = hit.collider.gameObject;
}
}
agent.SetDestination(this.transform.position);
agent.SetDestination(Vector3.zero);
transform.LookAt(investigateSpot);
if(timer >= investigateWait)
{
state = AIState.PATROL;
timer = 0;
}
}
function OnTriggerEnter(coll : Collider)
{
if (coll.tag == PlayerTagName)
{
state = AIState.INVESTIGATE;
investigateSpot = coll.gameObject.transform.position;
}
}