Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Enemy : MonoBehaviour {
- private string[] routines = {"Idle","Patrol","Search","Attack"};
- public int routelength;
- public Transform[] route;
- public int startroutine = 0;
- //Our current routine
- private int curro;
- private int cur = 0;
- public GameObject player;
- NavMeshAgent navma;
- int importance = 1;
- //Estimated position of target
- Vector3 estpos;
- //First position we are at
- Vector3 fipo;
- void Start(){
- fipo = transform.position;
- navma = this.GetComponent<NavMeshAgent>();
- curro = startroutine;
- for(int i = 0; i< routelength;i++){
- route[i].gameObject.GetComponent<MeshRenderer>().enabled = false;
- }
- }
- void Update(){
- if(routines[curro]=="Idle"){
- idle();
- }
- else if(routines[curro]=="Patrol"){
- patrol();
- }
- else if(routines[curro]=="Search"){
- search();
- }
- else if(routines[curro]=="Attack"){
- attack();
- }
- hear();
- see();
- }
- void idle(){
- navma.destination = fipo;
- }
- //In this routine, we patrol a given area.
- void patrol(){
- importance = 2;
- if(cur<routelength){
- Vector3 dest = route[cur].position;
- navma.destination = dest;
- if(targetDistance(route[cur].position)<2){
- cur++;
- }
- }
- else{
- cur = 0;
- }
- }
- //In this routine, we're investigating a sound.
- void search(){
- importance = 2;
- navma.destination = estpos;
- //We're at the estimated position
- if(targetDistance(estpos)<1){
- //We continue where we left of before seeing or hearing the player
- curro = startroutine;
- //Hear and See will change estpos, so if we're hearing or seeing the player right now, we won't do this.
- }
- }
- void attack(){
- importance = 4;
- navma.destination = player.transform.position;
- if(targetDistance(player.transform.position)<2){
- navma.destination = transform.position;
- }
- }
- //Our senses
- void hear(){
- //We hear the player (he is close), but not hard enough to drop everything we're doing
- //We're also not doing anything important
- if(targetDistance(player.transform.position)<15 && importance<2){
- //Chance of 1% (per frame) we're going to investigate
- //This actually determines the accuraccy of our estimated position
- if(Random.value<0.01){
- estpos = player.transform.position;
- curro = 2;
- }
- }
- //Player is quite close, we definitely heard him now.
- if(targetDistance(player.transform.position)<10 && importance<4){
- //Chance of 50% (per frame) that we update our estimated position
- if(Random.value<0.5){
- estpos = player.transform.position;
- }
- curro = 2;
- }
- }
- void see(){
- RaycastHit hit;
- if(Physics.Raycast(transform.position, Vector3.forward, out hit)){
- //We saw the player
- if(hit.collider.gameObject.name == player.name){
- print("saw player");
- //If close to the player, attack
- if(targetDistance(player.transform.position)<10){
- curro = 3;
- }
- else{
- //We remember where we last saw the player and investigate at that position
- estpos = player.transform.position;
- curro = 2;
- }
- }
- }
- }
- //Simple 3D Pythagoras
- float targetDistance(Vector3 target){
- float distance = 0;
- Vector3 distancev;
- distancev.x = target.x - transform.position.x;
- distancev.y = target.y - transform.position.y;
- distancev.z = target.z - transform.position.z;
- distance = Mathf.Sqrt(distancev.x*distancev.x + distancev.y*distancev.y);
- distance = Mathf.Sqrt(distance*distance + distancev.z*distancev.z);
- return distance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment