- Home /
How do I create AI with a good side and evil side?
Basically the script I have takes uses a trigger. when something with the tag player, GoodGuy, or Enemy collides with the trigger, the AI with the collider should follow the collidee. The problem is, after one object has collided with the trigger it for some reason wont work. Here is my script...
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class stalk : MonoBehaviour { public GameObject TheCollider; public GameObject DefaultFollow; public bool CanAttack; public bool Attack1; public bool Attack2; public bool CanFollow = true; public int teamnum; public float distance; public int colliderteamnum; public bool following; private Team teamScript; private AI aiScript; public Animator anim; void Start () { teamScript = this.gameObject.GetComponent (); aiScript = this.gameObject.GetComponent (); teamnum = teamScript.team; anim = GetComponent (); }
void Update () {
distance = Vector3.Distance (this.transform.position, TheCollider.transform.position);
// FOLLOWING MECHANICS
if (following) {
//follows the collided object
aiScript.agent.SetDestination (TheCollider.transform.position);
Vector3 eulerBefore = this.transform.eulerAngles; // or localEulerAngles, dependent on your needs
this.transform.LookAt (TheCollider.transform);
eulerBefore.y = this.transform.eulerAngles.y;
this.transform.eulerAngles = eulerBefore;
CanFollow = false;
aiScript.agent.speed = 0.4f;
anim.SetBool ("Walk", false);
anim.SetBool ("Run", true);
if (CanAttack) {
anim.SetBool ("Run", false);
anim.SetBool ("AttackA", true);
aiScript.agent.speed = 0f;
Invoke ("A1", 1f);
Invoke ("A2", 3.3f);
}
if (Attack1) {
anim.SetBool ("AttackB", true);
anim.SetBool ("AttackA", false);
Invoke ("A2", 1.5f);
}
if (Attack2) {
anim.SetBool ("AttackC", true);
anim.SetBool ("AttackB", false);
Invoke ("A3", 0.9f);
}
if (CanAttack == false) {
anim.SetBool ("Walk", false);
anim.SetBool ("Run", true);
anim.SetBool ("AttackA", false);
anim.SetBool ("AttackB", false);
anim.SetBool ("AttackC", false);
Attack1 = false;
Attack2 = false;
}
if (TheCollider.GetComponent<Life> ().dead == true) {
following = false;
}
}
if (following == false) {
anim.SetBool ("Walk", true);
anim.SetBool ("Run", false);
anim.SetBool ("AttackA", false);
anim.SetBool ("AttackB", false);
anim.SetBool ("AttackC", false);
aiScript.agent.speed = 0.1f;
CanFollow = true;
Attack1 = false;
Attack2 = false;
CanAttack = false;
}
//ATTACKING MECHANICS
if (distance < 3.3f) {
CanAttack = true;
}
if (distance > 3.4f) {
CanAttack = false;
}
}
public void OnTriggerStay(Collider other){
//tests to see if object is a player
if (CanFollow == true) {
if (other.tag == "Player" || other.tag == "GoodGuy" || other.tag == "Enemy") {
// sets the collider game object to the object that has collided with the trigger
if (colliderteamnum > teamnum || colliderteamnum < teamnum) {
following = true;
TheCollider = other.gameObject;
}
if (colliderteamnum == teamnum) {
following = false;
TheCollider = DefaultFollow;
}
// finds the TeamScript on the collider and turns colliderteamnum to that variable
colliderteamnum = TheCollider.gameObject.GetComponent<Team> ().team;
}
}
}
public void OnTriggerExit(Collider other){
if (other.tag == "Player" || other.tag == "GoodGuy" || other.tag == "Enemy") {
if (colliderteamnum > teamnum || colliderteamnum < teamnum) {
following = false;
TheCollider = DefaultFollow;
}
if (colliderteamnum == teamnum) {
following = false;
TheCollider = DefaultFollow;
}
}
}
public void A1(){
Attack1 = true;
}
public void A2(){
Attack2 = true;
Attack1 = false;
}
public void A3(){
Attack2 = false;
Attack1 = false;
}
}
The Collider does right to take into account the team numbers and the tags and the AI follows and attacks and everything but when another object collides with it, the object stops following all objects all together.. If anyone has a way they know to fix this it would be greatly appreciated, Thank you.