public float frontSensorAngle = 45f; //sets angle for secondary front sensor #endregion #endregion // Use this for initialization void Awake() { if (randomAttackState) //if designer chose to have a random attack state use this before anything else { rng = Random.Range(1, 51); if (rng <= 25) //if rng value is less than or equal to 25 select flee { SwitchTo = TypeofEnemy.Flee; } else //else if the value is higher choose to attack player { SwitchTo = TypeofEnemy.sawPlayer; } } motor_ = gameObject.GetComponent <TankMotor>(); //sets motor info from tankdata script tankInfo_ = gameObject.GetComponent <TankData>(); //sets speed info from tankdata script ammunition_ = gameObject.GetComponent <TankData>().Ammo; //sets ammo type from tankdata script tankTransform_ = gameObject.GetComponent <Transform>(); //grabs transform of tank originalState_ = EnemyType; //sets up an original state to default back to based off designer decision sensing = GetComponent <AISensoryScript>(); //grabs sensory input from AISensoryScript originPoint = gameObject.GetComponent <Transform>().position; //sets up origin point based off tank spawn to go back to (if applicable) origlife = gameObject.GetComponent <TankData>().tankHealth; // sets up life here to use origRespawnTime_ = respawnTime; //sets original respawn time to user defined respawn time sfxAudio = GetComponent <AudioSource>(); }
void SensedSomething() { if (sensing.CanSee(GameObject.FindWithTag("PowerUp"))) //if set to flee AI runs from it (can't figure it out at the moment) { playerCharacter_ = gameObject.transform; EnemyType = TypeofEnemy.sawPlayer; } if (sensing.CanSee(GameManagement.instance.player1)) { playerCharacter_ = GameManagement.instance.player1.transform; EnemyType = SwitchTo; } else if (sensing.CanHear(GameManagement.instance.player1)) { playerCharacter_ = GameManagement.instance.player1.transform; EnemyType = TypeofEnemy.HeardASound; } if (sensing.CanSee(GameManagement.instance.player2)) { playerCharacter_ = GameManagement.instance.player2.transform; EnemyType = SwitchTo; } else if (sensing.CanHear(GameManagement.instance.player2)) { playerCharacter_ = GameManagement.instance.player2.transform; EnemyType = TypeofEnemy.HeardASound; } }
void StateCheck() //checks time against developer set time to either stop searching or continue (MIGHT add in last position check) //assuming enemy DOESN'T have player in field of view anymore { if (sensing.CanSee(GameManagement.instance.player1.gameObject) || sensing.CanHear(GameManagement.instance.player1.gameObject) || sensing.CanSee(GameManagement.instance.player2.gameObject) || sensing.CanHear(GameManagement.instance.player2.gameObject)) { if (!searchTimeStarted_) //starts a search timer for when player is lost sight of (until either runs out or is seen again keep heading towards player) { StartCoroutine("SearchTime"); searchTimeStarted_ = true; } searchTimer_ = 0; //keeps resetting timer until it can't directly see or hear player anymore then goes back to normal } else if (!sensing.CanSee(GameManagement.instance.player1.gameObject) || !sensing.CanSee(GameManagement.instance.player2.gameObject) || avoiding) { if ((EnemyType == TypeofEnemy.Flee && ((searchTimeAllowed + 5) <= searchTimer_)) || EnemyType != TypeofEnemy.Flee && searchTimeAllowed <= searchTimer_) //EnemyType check is to see if enemy was selected as flee or otherwise. if fleeing add an additional 10 seconds to count for otherwise //leave alone { searchTimeStarted_ = false; playerCharacter_ = null; avoiding = false; StopCoroutine("SearchTime"); searchTimer_ = 0; EnemyType = originalState_; } } }
void AILife() { if (tankInfo_.tankHealth <= 0 && respawnTime >= 0) { if (respawnTime == origRespawnTime_) //error catch so tank destruction sfx doesn't repeat over and over { sfxAudio.PlayOneShot(GameManagement.instance.tankDestruction, GameManagement.instance.sfxVolume); } EnemyType = originalState_; respawnTime -= Time.deltaTime; playerCharacter_ = null; gameObject.GetComponent <CharacterController>().enabled = false; gameObject.transform.GetChild(0).gameObject.SetActive(false); motor_.enabled = false; } else if (respawnTime <= 0) { respawnTime = origRespawnTime_; tankTransform_.position = originPoint; tankTransform_.rotation = Quaternion.Euler(0, 0, 0); tankInfo_.tankHealth = origlife; gameObject.GetComponent <CharacterController>().enabled = true; gameObject.transform.GetChild(0).gameObject.SetActive(true); motor_.enabled = true; } }
void StateCheck() //checks time against developer set time to either stop searching or continue (MIGHT add in last position check) //assuming enemy DOESN'T have player in field of view anymore { if (sensing.CanSee(GameManagement.instance.player1.gameObject) || sensing.CanHear(GameManagement.instance.player1.gameObject)) { if (!searchTimeStarted_) { StartCoroutine("SearchTime"); searchTimeStarted_ = true; } searchTimer_ = 0; } else if (!sensing.CanSee(GameManagement.instance.player1.gameObject) || avoiding) { if ((EnemyType == TypeofEnemy.Flee && ((searchTimeAllowed + 5) <= searchTimer_)) || EnemyType != TypeofEnemy.Flee && searchTimeAllowed <= searchTimer_) //EnemyType check is to see if enemy was selected as flee or otherwise. if fleeing add an additional 10 seconds to count for otherwise //leave alone { searchTimeStarted_ = false; playerCharacter_ = null; avoiding = false; StopCoroutine("SearchTime"); searchTimer_ = 0; EnemyType = originalState_; } } }
// Update is called once per frame void FixedUpdate() { ObstacleAvoidance(); AILife(); switch (EnemyType) { case TypeofEnemy.Idle: { if (!avoiding) { Idle(); SensedSomething(); } if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } return; } case TypeofEnemy.Looping: { if (!avoiding) { TurningAndMovingAI(); Looping(); SensedSomething(); } if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } return; } case TypeofEnemy.Retracing: { if (!avoiding) { TurningAndMovingAI(); Retrace(); SensedSomething(); } else if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } return; } case TypeofEnemy.Lookout: { if (!avoiding) { LookoutAI(); SensedSomething(); } else if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } return; } case TypeofEnemy.Flee: { if (avoiding) { ObstacleAvoidance(); } else if (!avoiding) { RunFATBOYRun(); } return; } case TypeofEnemy.sawPlayer: { if (avoiding) { ObstacleAvoidance(); } else if (!avoiding) { FoundYou(); } return; } case TypeofEnemy.HeardASound: { if (!avoiding) { HeardSomething(); if (sensing.CanSee(GameManagement.instance.player1)) { playerCharacter_ = GameManagement.instance.player1.transform; EnemyType = SwitchTo; } else if (!sensing.CanHear(GameManagement.instance.player1)) { EnemyType = originalState_; } } else if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } return; } default: { Debug.Log("No/Invalid State Was Selected"); break; } } }
// Update is called once per frame void FixedUpdate() { ObstacleAvoidance(); AILife(); switch (EnemyType) { case TypeofEnemy.Idle: { if (!avoiding) { Idle(); if (sensing.CanSee(GameManagement.instance.player1)) { playerCharacter_ = GameManagement.instance.player1.transform; EnemyType = SwitchTo; } if (sensing.CanHear(GameManagement.instance.player1)) { EnemyType = TypeofEnemy.HeardASound; } } if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } //if hearSomething //investigatesound return; } case TypeofEnemy.Looping: { if (!avoiding) { TurningAndMovingAI(); Looping(); if (sensing.CanSee(GameManagement.instance.player1)) { playerCharacter_ = GameManagement.instance.player1.transform; EnemyType = SwitchTo; } if (sensing.CanHear(GameManagement.instance.player1)) { EnemyType = TypeofEnemy.HeardASound; } } if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } //if hearSomething //investigatesound return; } case TypeofEnemy.Retracing: { if (!avoiding) { TurningAndMovingAI(); Retrace(); if (sensing.CanSee(GameManagement.instance.player1)) { playerCharacter_ = GameManagement.instance.player1.transform; EnemyType = SwitchTo; } if (sensing.CanSee(GameObject.FindWithTag("PowerUp"))) //if set to flee AI runs from it (can't figure it out at the moment) { playerCharacter_ = gameObject.transform; EnemyType = TypeofEnemy.sawPlayer; } if (sensing.CanHear(GameManagement.instance.player1)) { EnemyType = TypeofEnemy.HeardASound; } } else if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } //if hearSomething //investigatesound return; } case TypeofEnemy.Lookout: { if (!avoiding) { LookoutAI(); if (sensing.CanSee(GameManagement.instance.player1)) { playerCharacter_ = GameManagement.instance.player1.transform; EnemyType = SwitchTo; } if (sensing.CanHear(GameManagement.instance.player1)) { EnemyType = TypeofEnemy.HeardASound; } } else if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } //if hearSomething //investigatesound return; } case TypeofEnemy.Flee: { if (avoiding) { ObstacleAvoidance(); } else if (!avoiding) { RunFATBOYRun(); } return; } case TypeofEnemy.sawPlayer: { if (avoiding) { ObstacleAvoidance(); } else if (!avoiding) { FoundYou(); } return; } case TypeofEnemy.HeardASound: { if (!avoiding) { HeardSomething(); if (sensing.CanSee(GameManagement.instance.player1)) { playerCharacter_ = GameManagement.instance.player1.transform; EnemyType = SwitchTo; } else if (!sensing.CanHear(GameManagement.instance.player1)) { EnemyType = originalState_; } } else if (avoiding) { if (EnemyType == originalState_) { avoiding = false; motor_.Rotation(tankInfo_.turnSpeed); return; } else { ObstacleAvoidance(); } } //if hearSomething //investigatesound return; } default: { Debug.Log("No/Invalid State Was Selected"); break; } } }