bool CheckForSightOnTarget(GameObject target) { Debug.Log("Me: " + gameObject); Debug.Log("Them: " + target); GameObject originalTarg = target; AIThreat threat = target.GetComponent <AIThreat>(); if (threat != null) { target = threat.head; } float viewDistance = 50; GameObject aim = weaponManager.GetAimObject(); aim.transform.LookAt(target.transform); RaycastHit[] LOS = Physics.RaycastAll(aim.transform.position, aim.transform.forward, viewDistance); Array.Sort(LOS, SortByDistance); GameObject seen = null; for (int x = 0; x < LOS.Length; x++) { if (LOS[x].collider.isTrigger == false && LOS[x].collider.gameObject != gameObject) { seen = LOS[x].collider.gameObject; break; } } return(seen == originalTarg); }
public void FireAt(GameObject target) { AIThreat threat = target.GetComponent <AIThreat>(); if (threat != null) { target = threat.head; } aim.transform.LookAt(target.transform); Debug.DrawRay(aim.transform.position, aim.transform.forward, Color.red, 10, true); if (equippedWeapon != null) { /* if (Input.GetMouseButton(0)) { * Shoot(); * } else * anim.SetBool("Shooting", false); */ Shoot(); } else { Punch(); } }
AIThreat GetClosestThreat() { AIThreat closest = threats[0]; foreach (AIThreat threat in threats) { if (threat.gameObject != gameObject) // if its not me { return(threat); } } Debug.Log("SHOULD NEVER HAPPEN"); return(null); // should never happen... }
IEnumerator AILoop() { while (status.isDead == false) { threats = new List <AIThreat>(threatsGLOBAL); threats.Sort(SortThreatsByDistanceFromSelf); yield return(null); int count = threats.Count; threats.RemoveRange(count / 2 + (count % 2 == 0 ? 0 : 1), count / 2); AIThreat target = GetClosestThreat(); yield return(null); float dist = gameObject.DistanceBetweenSqr(target.gameObject); if (status.health < 20) { SetAimTarget(null); goal = Goals.HIDE; } else if ((lootLevel < 8 || inventory.GetAmmoInventorySize() <= 2) && dist > Mathf.Pow(5, 2)) { SetAimTarget(null); goal = Goals.LOOT; OnGoalUpdated(); } else if (CheckForSightOnTarget(target.gameObject)) { if (aggression < 5 && dist < Mathf.Pow(10, 2)) { SetAimTarget(null); goal = Goals.FLEE; } else { SetAimTarget(target.gameObject); goal = Goals.ATTACK; } } else { SetAimTarget(target.gameObject); goal = Goals.STALK; } yield return(new WaitForSeconds(3)); } }
public void Init() { Terrain ground = GameObject.FindGameObjectWithTag("Ground").GetComponent <Terrain>(); TerrainData groundData = ground.terrainData; Vector3 groundSize = groundData.size; threats = new List <AIThreat>(); wall = GameObject.Find("BlueWall"); float randx = 0f; float randz = 0f; float height = 0f; GameObject enemy = (GameObject)Resources.Load("Prefabs/Characters/Enemy"); for (int i = 0; i < enemyCount; ++i) { randx = Random.Range(-groundSize.x / 2, groundSize.x / 2); randz = Random.Range(-groundSize.z / 2, groundSize.z / 2); height = ground.SampleHeight(new Vector3(randx, 0f, randz)); GameObject bruh = Instantiate(enemy, new Vector3(randx, height, randz), Quaternion.identity); AIThreat ethreat = bruh.GetComponent <AIThreat>(); if (ethreat != null) { threats.Add(ethreat); } } GameObject player = GameObject.FindGameObjectWithTag("Player"); randx = Random.Range(-groundSize.x / 2, groundSize.x / 2); randz = Random.Range(-groundSize.z / 2, groundSize.z / 2); height = ground.SampleHeight(new Vector3(randx, 0f, randz)); player.transform.position = new Vector3(randx, height, randz); AIThreat pthreat = player.GetComponent <AIThreat>(); if (pthreat != null) { threats.Add(pthreat); } wall.GetComponent <Wall> ().SetAllowedToMove(true); }
int SortThreatsByDistanceFromSelf(AIThreat g1, AIThreat g2) // radius biggest to small { return(g1.gameObject.DistanceBetween(gameObject).CompareTo(g2.gameObject.DistanceBetween(gameObject))); }
bool CheckForLineOfSight(GameObject one, GameObject two, float viewDistance = float.MaxValue, bool debug = false) { GameObject firstComp; GameObject secondComp; AIThreat threatOne = one.GetComponent <AIThreat>(); AIThreat threatTwo = two.GetComponent <AIThreat>(); Waypoint wayOne = one.GetComponent <Waypoint>(); Waypoint wayTwo = two.GetComponent <Waypoint>(); if (threatOne != null) { firstComp = threatOne.head; } else if (wayOne != null) { firstComp = wayOne.GetViewpoint(); } else { firstComp = one; } if (threatTwo != null) { secondComp = threatTwo.head; } else if (wayTwo != null) { secondComp = wayTwo.GetViewpoint(); } else { secondComp = two; } GameObject aim = new GameObject(); aim.transform.position = firstComp.transform.position; aim.transform.LookAt(secondComp.transform); RaycastHit[] LOS = Physics.RaycastAll(aim.transform.position, aim.transform.forward, viewDistance); Array.Sort(LOS, SortByDistance); GameObject seen = null; for (int x = 0; x < LOS.Length; x++) { if ((LOS[x].collider.isTrigger == false || LOS[x].collider.gameObject.CompareTag("Waypoint")) && LOS[x].collider.gameObject != gameObject) { seen = LOS[x].collider.gameObject; if (debug) { DrawSomeStuff(firstComp, seen, LOS[x]); } break; } } Destroy(aim); return(seen == two); }