- Home /
Question by
UEGnasher · Mar 18, 2016 at 12:40 PM ·
build-error
Final Build problem
In my project I have a couple of Ai patrolling around randomly. In the preview, they walk and react whenever my fps controller comes within the intended range. Whenever I build my game whether it be for PC or Mac, the Ai start to slide and longer react to the controller. I've tried restarting Unity, reimporting all assets, and re-doing the code but to no avail. Is there anyway to fix this issue? If there is, help will be greatly appreciated. Thank you!!
Here is the code for my Ai patrol:
using UnityEngine; using System.Collections;
namespace UnityStandardAssets.Characters.ThirdPerson {
public class EnemyState : MonoBehaviour {
public NavMeshAgent agent;
public ThirdPersonCharacter character;
public enum State {
PATROL,
CHASE
}
public State state;
private bool alive;
// Patrol Variables
public GameObject[] waypoints;
private int waypointInd;
public float patrolSpeed = 0.5f;
// Chase Variables
public float chaseSpeed = 1f;
public GameObject target;
void Start () {
agent = GetComponent<NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
agent.updatePosition = true;
agent.updateRotation = false;
waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
waypointInd = Random.Range(0, waypoints.Length);
state = EnemyState.State.PATROL;
alive = true;
StartCoroutine("FSM");
}
IEnumerator FSM() {
while (alive)
{
switch (state)
{
case State.PATROL:
Patrol ();
break;
case State.CHASE:
Chase ();
break;
}
yield return null;
}
}
void Patrol() {
agent.speed = patrolSpeed;
if ( Vector3.Distance (this.transform.position, waypoints[waypointInd].transform.position) >= 2)
{
agent.SetDestination(waypoints[waypointInd].transform.position);
character.Move(agent.desiredVelocity, false, false);
}
else if (Vector3.Distance (this.transform.position, waypoints[waypointInd].transform.position) <= 2)
{
waypointInd = Random.Range(0, waypoints.Length);
}
else
{
character.Move (Vector3.zero, false, false);
}
}
void Chase()
{
agent.speed = chaseSpeed;
agent.SetDestination(target.transform.position);
character.Move(agent.desiredVelocity, false, false);
}
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "Player")
{
state = EnemyState.State.CHASE;
target = col.gameObject;
}
}
}
}
Comment