Hello
This question has got me stumped. One of my AI’s will not attack the other AI. The Human soldier has troubles using raycasts to find his target.The game always says the enemy is behind a wall. But oddly enough if I make the Human soldier’s faction into the Aliens and give them an alien health script they attack fine. I have everything on both the Aliens and Humans to ignore raycasts besides the their health.
Also for another note this script works in another game where I created this script (hence why the health is called Russians and Americans). I am at my wits end, I can’t figure out why this is happening. If anyone could be of any help. I’d be very grateful.
I know it’s silly I’m posting my whole script here, but I am so at a lost for this, I just so desperate for help. Please forgive me.
public class RangedAI : MonoBehaviour {
public GameObject Waypoint;
public string EnemyFaction;
public string FriendlyFaction;
public GameObject Target;
public GameObject ClosestEnemy;
public bool Blocked;
UnityEngine.AI.NavMeshAgent agent;
public bool BehindWall;
private bool Attacking;
public float AttackTime =2f;
private AmericanHP americanhp;
private RussianHP russianhp;
public GameObject Barrel;
public GameObject Bullet;
private bool Attackable;
public float ShootTime;
private float ShootTimeMin = 1f;
private float ShootTimeMax = 3f;
public bool ActiveDead;
private float Timer =3f;
public float distance1;
public float distance2;
void Awake ()
{
agent = GetComponentInParent <UnityEngine.AI.NavMeshAgent> ();
ShootTime = Random.Range (ShootTimeMin, ShootTimeMax);
if (EnemyFaction == ("Aliens"))
{
americanhp = GetComponentInChildren <AmericanHP> ();
}
if (EnemyFaction == ("Humans"))
{
russianhp = GetComponentInChildren <RussianHP> ();
}
}
GameObject FindClosestEnemy()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag(EnemyFaction);
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
RaycastHit hit;
BehindWall = true;
if (go.transform.FindChild ("Marksmen") == true)
{
Attackable = false;
}
if (go.transform.FindChild ("Marksmen") == false)
{
Attackable = true;
}
if (Physics.Raycast (transform.position, (go.transform.position - transform.position), out hit) && hit.collider.gameObject.tag == (EnemyFaction))
{
BehindWall = false;
}
if (Physics.Raycast (transform.position, (go.transform.position - transform.position), out hit) && hit.collider.gameObject.tag == ("Wall"))
{
BehindWall = true;
}
if (curDistance < distance && BehindWall == false && Attackable == true)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
void Update ()
{
if (FriendlyFaction == ("Humans") && americanhp.Dead == true && ActiveDead == false)
{
ActiveDead = true;
gameObject.tag = ("Killed");
GetComponent<Animation> ().Play ("Death");
agent.Stop ();
}
if (FriendlyFaction == ("Aliens") && russianhp.Dead == true && ActiveDead == false)
{
ActiveDead = true;
gameObject.tag = ("Killed");
GetComponent<Animation> ().Play ("Death");
agent.Stop ();
}
if (ActiveDead == false)
{
if (Target != null && Target.tag == ("Killed"))
{
Target = null;
}
if (Attacking == true && AttackTime == 2f)
{
GetComponent<Animation> ().Stop ("Idle");
GetComponent<Animation> ().Play ("Attack");
}
if (Waypoint == null && Target == null)
{
GetComponent<Animation> ().Play ("Idle");
agent.Stop ();
}
if (Target == null)
{
ClosestEnemy = FindClosestEnemy ();
}
if (Target == null && ClosestEnemy != null)
{
float dist = Vector3.Distance (ClosestEnemy.transform.position, transform.position);
if (dist <= 70)
{
RaycastHit hit;
if (Physics.Raycast (transform.position, (ClosestEnemy.transform.position - transform.position), out hit) && hit.collider.gameObject.tag != ("Wall"))
{
Target = ClosestEnemy;
}
}
if (dist >= 71)
{
Target = null;
}
}
if (Target == null)
{
Target = null;
}
if (Target != null)
{
float dist = Vector3.Distance (ClosestEnemy.transform.position, transform.position);
if (dist >= 71)
{
Target = null;
}
}
if (Target == null && Waypoint != null && Attacking == false)
{
agent.SetDestination (Waypoint.transform.position);
agent.Resume ();
if (Attacking == false)
{
GetComponent<Animation> ().Play ("Run");
}
}
if (Target != null && Waypoint == null)
{
agent.SetDestination (Target.transform.position);
agent.Resume ();
if (Attacking == false)
{
GetComponent<Animation> ().Play ("Run");
}
}
if (Target != null && Blocked == true)
{
agent.SetDestination (Target.transform.position);
agent.Resume ();
if (Attacking == false)
{
GetComponent<Animation> ().Play ("Run");
}
}
if (Target != null && Blocked == false)
{
float dist2 = Vector3.Distance (Target.transform.position, transform.position);
if (dist2 <= distance1)
{
agent.Stop ();
if (Attacking == false)
{
GetComponent<Animation> ().Play ("Idle");
}
}
}
if (Target != null)
{
RaycastHit hit;
if (Physics.Raycast (transform.position, (Target.transform.position - transform.position), out hit) && hit.collider.gameObject == (Target))
{
Blocked = false;
}
if (Physics.Raycast (transform.position, (Target.gameObject.transform.position - transform.position), out hit) && hit.collider.gameObject.tag == ("Wall"))
{
Blocked = true;
}
}
}
}
void FixedUpdate ()
{
if (ActiveDead == false)
{
if (Attacking == true)
{
AttackTime -= Time.deltaTime;
}
if (AttackTime <= 0)
{
Attacking = false;
AttackTime = 2f;
}
if (Target != null)
{
float dist3 = Vector3.Distance (Target.transform.position, transform.position);
if (dist3 <= distance2)
{
if (Target != null && Blocked == false && Attacking == false)
{
ShootTime -= Time.deltaTime;
transform.LookAt(new Vector3(Target.transform.position.x, transform.position.y, Target.transform.position.z));
}
if (Target != null && Waypoint == null && dist3 >=distance2)
{
agent.SetDestination (Target.transform.position);
agent.Resume ();
GetComponent<Animation> ().Play ("Run");
if (Attacking == false)
{
GetComponent<Animation> ().Play ("Run");
}
}
if (ShootTime <= 0)
{
Attacking = true;
Instantiate (Bullet, Barrel.transform.position, Barrel.transform.rotation);
ShootTime = Random.Range (ShootTimeMin, ShootTimeMax);
}
}
}
}
}
void OnTriggerEnter (Collider other)
{
if (ActiveDead == false)
{
if (other.gameObject == Waypoint) {
Waypoint = null;
agent.Stop ();
GetComponent<Animation> ().Play ("Idle");
}
}
}
}