Unity3D AI人工智能(坦克模型自动寻怪,自动追击)

功能:
 
1,坦克自动旋转炮管,自动瞄准
2,在坦克的射程之内自动开炮
3,坦克自动向你追击
 
unity3d脚本:
view plaincopy to clipboard
01.var Enemy_tank: GameObject; //定义坦克  
02.var firepoint: Transform; //定义开火点  
03.var bullet: Rigidbody; //定义子弹  
04.var bullspeed: int=50; //定义子弹速度  
05.var attackRange = 100.0; //定义距离  
06.var target : Transform;//定义共计目标  
07.//static var value : float ;  
08.private var cun_time: int=0;  
09.function Start () //初始化目标  
10.{  
11.if (target == null && GameObject.FindWithTag("hero")) target = GameObject.FindWithTag("hero").transform;  
12.}  
13.function Update ()   
14.{  
15.//Tank Fire  
16.// cun_time++;  
17.// if(cun_time==260)  
18.// {   
19.// var clone : Rigidbody;  
20.// clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);  
21.// clone.velocity = transform.TransformDirection (Vector3.right*bullspeed);  
22.// cun_time=0;  
23.// }  
24.//distance from A and B  
25.//angle from A and B  
26.//var targetDir = transform.position-other.position;  
27.//var right = transform.right;  
28.//var angle = Vector3.Angle(targetDir, right);  
29.//print("angle is ="+angle);//if (angle < 5.0)//transform.Rotate(Vector3.up*0.5);  
30.//var speed = 0.1;  
31.//transform.rotation =Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);  
32.  
33.  
34.Enemy_tank=GameObject.Find("Enemy_barrel"); //实例化  
35.if (target == null)  
36.return;  
37.if (!CanSeeTarget ())  
38.return;  
39.// Rotate towards target  
40.var targetPoint = target.position;  
41.var targetRotation = Quaternion.LookRotation (targetPoint - Enemy_tank.transform.position,Vector3.up); //求出与目标之间的角度  
42.Enemy_tank.transform.rotation = Quaternion.Slerp(Enemy_tank.transform.rotation, targetRotation, Time.deltaTime * 2.0); //旋转之角度与目标对齐  
43.//If we are almost rotated towards target - fire one clip of ammo  
44.var forward = Enemy_tank.transform.TransformDirection(Vector3.forward);  
45.var targetDir =Enemy_tank.transform.position-target.position; //求出2者之间的距离  
46.if((Vector3.Angle(forward, targetDir) <10.0)||((Vector3.Angle(forward, targetDir) >-10.0))) //这边是自动开炮的效果  
47.{  
48.cun_time++;  
49.if(cun_time==260)  
50.{   
51.var clone : Rigidbody;  
52.clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);  
53.clone.velocity = Enemy_tank.transform.TransformDirection (Vector3.forward*bullspeed);  
54.cun_time=0;  
55.}  
56.}  
57.if((Vector3.Distance(transform.position, target.position) < attackRange)) //发现目标而且在范围之内,开始追击目标  
58.{  
59.var targetPoint1 = target.position;  
60.//Calculation the angle with target  
61.var targetRotation1 = Quaternion.LookRotation (targetPoint1 - transform.position,Vector3.up);  
62.//Rotated Itself then make a line with target  
63.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation1, Time.deltaTime * 1.0);  
64.transform.Translate(Vector3.forward*0.05);  
65.}  
66.}  
67.function CanSeeTarget (): System.Boolean //计算是否被发现  
68.{  
69.if (Vector3.Distance(transform.position, target.position) > attackRange) return false;  
70.var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit))  
71.return hit.transform == target; return false;  
72.}  

你可能感兴趣的:(unity3d)