View difference between Paste ID: yf5QPjeQ and KNCgPBMU
SHOW: | | - or go back to the newest paste.
1
using UnityEngine;
2
using System.Collections;
3
4-
/*
4+
5-
 * AI system for Unity3D, NavMesh powered
5+
6-
 * © Robin van Ee 2012
6+
7-
 * Use with permission per project unless otherwise licensed
7+
8-
 * Contact [email protected] for licensing options
8+
9-
*/
9+
10
	private int curro;
11
	private int cur = 0;
12
	public GameObject player;
13
	NavMeshAgent navma;
14
	int importance = 1;
15
	//Estimated position of target
16
	Vector3 estpos;
17
	//First position we are at
18
	Vector3 fipo;
19
	
20
	void Start(){
21
		fipo = transform.position;
22
		navma = this.GetComponent<NavMeshAgent>();
23
		curro = startroutine;
24
		for(int i = 0; i< routelength;i++){
25
			route[i].gameObject.GetComponent<MeshRenderer>().enabled = false;
26
		}
27
	}
28
	void Update(){
29
		if(routines[curro]=="Idle"){
30
			idle();
31
		}
32
		else if(routines[curro]=="Patrol"){
33
			patrol();
34
		}
35
		else if(routines[curro]=="Search"){
36
			search();
37
		}
38
		else if(routines[curro]=="Attack"){
39
			attack();
40
		}
41
		hear();
42
		see();
43
	}
44
	
45
	void idle(){
46
		navma.destination = fipo;
47
	}
48
	
49
	//In this routine, we patrol a given area.
50
	void patrol(){
51
		importance = 2;
52
		if(cur<routelength){
53
			Vector3 dest = route[cur].position;
54
			navma.destination = dest;
55
			if(targetDistance(route[cur].position)<2){
56
				cur++;
57
			}
58
		}
59
		else{
60
			cur = 0;
61
		}
62
	}
63
	
64
	//In this routine, we're investigating a sound.
65
	void search(){
66
		importance = 2;
67
		navma.destination = estpos;
68
		//We're at the estimated position
69
		if(targetDistance(estpos)<1){
70
			//We continue where we left of before seeing or hearing the player
71
			curro = startroutine;
72
			//Hear and See will change estpos, so if we're hearing or seeing the player right now, we won't do this.
73
		}
74
	}
75
	
76
	void attack(){
77
		importance = 4;
78
		navma.destination = player.transform.position;
79
		if(targetDistance(player.transform.position)<2){
80
			navma.destination = transform.position;
81
		}
82
	}
83
	
84
	
85
	
86
	//Our senses
87
	void hear(){
88
		//We hear the player (he is close), but not hard enough to drop everything we're doing
89
		//We're also not doing anything important
90
		if(targetDistance(player.transform.position)<15 && importance<2){
91
			//Chance of 1% (per frame) we're going to investigate
92
			//This actually determines the accuraccy of our estimated position
93
			if(Random.value<0.01){
94
				estpos = player.transform.position;
95
				curro = 2;
96
			}
97
		}
98
		//Player is quite close, we definitely heard him now.
99
		if(targetDistance(player.transform.position)<10 && importance<4){
100
			//Chance of 50% (per frame) that we update our estimated position
101
			if(Random.value<0.5){
102
				estpos = player.transform.position;
103
			}
104
			curro = 2;
105
		}
106
	}
107
	
108
	void see(){
109
		RaycastHit hit;
110
		if(Physics.Raycast(transform.position, Vector3.forward, out hit)){
111
			//We saw the player
112
			if(hit.collider.gameObject.name == player.name){
113
				print("saw player");
114
				//If close to the player, attack
115
				if(targetDistance(player.transform.position)<10){
116
					curro = 3;
117
				}
118
				else{
119
					//We remember where we last saw the player and investigate at that position
120
					estpos = player.transform.position;
121
					curro = 2;
122
				}
123
			}
124
		}
125
	}
126
	
127
	//Simple 3D Pythagoras
128
	float targetDistance(Vector3 target){
129
		float distance = 0;
130
		Vector3 distancev;
131
		distancev.x = target.x - transform.position.x;
132
		distancev.y = target.y - transform.position.y;
133
		distancev.z = target.z - transform.position.z;
134
		distance = Mathf.Sqrt(distancev.x*distancev.x + distancev.y*distancev.y);
135
		distance = Mathf.Sqrt(distance*distance + distancev.z*distancev.z);
136
		return distance;
137
	}
138
}