a算法自动寻论 unity3d
2014-10-23 15:23:11 u013510614 阅读数 1019
var Enemy_tank: GameObject; //定义坦克
var firepoint: Transform; //定义开火点
var bullet: Rigidbody; //定义子弹
var bullspeed: int=50; //定义子弹速度
var attackRange = 100.0; //定义距离
var target : Transform;//定义共计目标
//static var value : float ;
private var cun_time: int=0;
function Start () //初始化目标
{
if (target == null && GameObject.FindWithTag("hero")) target = GameObject.FindWithTag("hero").transform;
}
function Update ()
{
//Tank Fire
// cun_time++;
// if(cun_time==260)
// {
// var clone : Rigidbody;
// clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);
// clone.velocity = transform.TransformDirection (Vector3.right*bullspeed);
// cun_time=0;
// }
//distance from A and B
//angle from A and B
//var targetDir = transform.position-other.position;
//var right = transform.right;
//var angle = Vector3.Angle(targetDir, right);
//print("angle is ="+angle);//if (angle < 5.0)//transform.Rotate(Vector3.up*0.5);
//var speed = 0.1;
//transform.rotation =Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);


Enemy_tank=GameObject.Find("Enemy_barrel"); //实例化
if (target == null)
return;
if (!CanSeeTarget ())
return;
// Rotate towards target
var targetPoint = target.position;
var targetRotation = Quaternion.LookRotation (targetPoint - Enemy_tank.transform.position,Vector3.up); //求出与目标之间的角度
Enemy_tank.transform.rotation = Quaternion.Slerp(Enemy_tank.transform.rotation, targetRotation, Time.deltaTime * 2.0); //旋转之角度与目标对齐
//If we are almost rotated towards target - fire one clip of ammo
var forward = Enemy_tank.transform.TransformDirection(Vector3.forward);
var targetDir =Enemy_tank.transform.position-target.position; //求出2者之间的距离
if((Vector3.Angle(forward, targetDir) <10.0)||((Vector3.Angle(forward, targetDir) >-10.0))) //这边是自动开炮的效果
{
cun_time++;
if(cun_time==260)
{
var clone : Rigidbody;
clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);
clone.velocity = Enemy_tank.transform.TransformDirection (Vector3.forward*bullspeed);
cun_time=0;
}
}
if((Vector3.Distance(transform.position, target.position) < attackRange)) //发现目标而且在范围之内,开始追击目标
{
var targetPoint1 = target.position;
//Calculation the angle with target
var targetRotation1 = Quaternion.LookRotation (targetPoint1 - transform.position,Vector3.up);
//Rotated Itself then make a line with target
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation1, Time.deltaTime * 1.0);
transform.Translate(Vector3.forward*0.05);
}
}
function CanSeeTarget (): System.Boolean //计算是否被发现
{
if (Vector3.Distance(transform.position, target.position) > attackRange) return false;
var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit))
return hit.transform == target; return false;
}
2012-08-23 11:12:22 xy849288321 阅读数 792

功能:

1,坦克自动旋转炮管,自动瞄准
2,在坦克的射程之内自动开炮
3,坦克自动向你追击

  1. var Enemy_tank: GameObject; //定义坦克  
  2. var firepoint: Transform; //定义开火点  
  3. var bullet: Rigidbody; //定义子弹  
  4. var bullspeed: int=50; //定义子弹速度  
  5. var attackRange = 100.0; //定义距离  
  6. var target : Transform;//定义共计目标  
  7. //static var value : float ;  
  8. private var cun_time: int=0;  
  9. 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;  


本文来自:http://blog.csdn.net/zhulinpptor/article/details/5882905

2014-07-09 10:47:38 u011714480 阅读数 638
功能:
 
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.}  
2013-03-13 16:31:31 fzhlee 阅读数 3802
功能:

1,坦克自动旋转炮管,自动瞄准
2,在坦克的射程之内自动开炮
3,坦克自动向你追击


var Enemy_tank: GameObject; //定义坦克  
var firepoint: Transform; //定义开火点  
var bullet: Rigidbody; //定义子弹  
var bullspeed: int=50; //定义子弹速度  
var attackRange = 100.0; //定义距离  
var target : Transform;//定义共计目标  
//static var value : float ;  
private var cun_time: int=0;  
function Start () //初始化目标  
{  
  if (target == null && GameObject.FindWithTag("hero")) target = GameObject.FindWithTag("hero").transform;  
}  
function Update ()   
{  
//Tank Fire  
// cun_time++;  
// if(cun_time==260)  
// {   
// var clone : Rigidbody;  
// clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);  
// clone.velocity = transform.TransformDirection (Vector3.right*bullspeed);  
// cun_time=0;  
// }  
//distance from A and B  
//angle from A and B  
//var targetDir = transform.position-other.position;  
//var right = transform.right;  
//var angle = Vector3.Angle(targetDir, right);  
//print("angle is ="+angle);//if (angle < 5.0)//transform.Rotate(Vector3.up*0.5);  
//var speed = 0.1;  
//transform.rotation =Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);  
  
  
Enemy_tank=GameObject.Find("Enemy_barrel"); //实例化  
if (target == null)  
return;  
if (!CanSeeTarget ())  
return;  
// Rotate towards target  
var targetPoint = target.position;  
var targetRotation = Quaternion.LookRotation (targetPoint - Enemy_tank.transform.position,Vector3.up); //求出与目标之间的角度  
Enemy_tank.transform.rotation = Quaternion.Slerp(Enemy_tank.transform.rotation, targetRotation, Time.deltaTime * 2.0); //旋转之角度与目标对齐  
//If we are almost rotated towards target - fire one clip of ammo  
var forward = Enemy_tank.transform.TransformDirection(Vector3.forward);  
var targetDir =Enemy_tank.transform.position-target.position; //求出2者之间的距离  
if((Vector3.Angle(forward, targetDir) <10.0)||((Vector3.Angle(forward, targetDir) >-10.0))) //这边是自动开炮的效果  
{  
cun_time++;  
if(cun_time==260)  
{   
var clone : Rigidbody;  
clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);  
clone.velocity = Enemy_tank.transform.TransformDirection (Vector3.forward*bullspeed);  
cun_time=0;  
}  
}  
if((Vector3.Distance(transform.position, target.position) < attackRange)) //发现目标而且在范围之内,开始追击目标  
{  
var targetPoint1 = target.position;  
//Calculation the angle with target  
var targetRotation1 = Quaternion.LookRotation (targetPoint1 - transform.position,Vector3.up);  
//Rotated Itself then make a line with target  
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation1, Time.deltaTime * 1.0);  
transform.Translate(Vector3.forward*0.05);  
}  
}  
function CanSeeTarget (): System.Boolean //计算是否被发现  
{  
if (Vector3.Distance(transform.position, target.position) > attackRange) return false;  
var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit))  
return hit.transform == target; return false;  
}  

2015-08-17 11:14:56 andyhebear 阅读数 1690
var Enemy_tank: GameObject;                                             //定义的坦克
 
var firepoint: Transform;                                                         //定义的开火点
 
var bullet: Rigidbody;                                                              //定义的子弹
 
var bullspeed: int=50;                                                              //定义的子弹速度
 
var attackRange = 100.0;                                                          //定义的距离
 
var target : Transform;//定义的共计目标
 
//static var value : float ;
 
private var cun_time: int=0;
 
function Start ()                                                                                 //初始化目标
 
{
 
if (target == null && GameObject.FindWithTag(“hero”))     target = GameObject.FindWithTag(“hero”).transform;
 
}
 
function Update ()
 
{
 
//Tank Fire
 
// cun_time++;
 
// if(cun_time==260)
 
// {
 
//     var clone : Rigidbody;
 
//     clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);
 
//     clone.velocity = transform.TransformDirection (Vector3.right*bullspeed);
 
//     cun_time=0;
 
//     }
 
//distance from A and B
 
//angle from A and B
 
//var targetDir = transform.position-other.position;
 
//var right = transform.right;
 
//var angle = Vector3.Angle(targetDir, right);
 
//print(“angle is =”+angle);//if (angle < 5.0)//transform.Rotate(Vector3.up*0.5);
 
//var speed = 0.1;
 
//transform.rotation =Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);
 
Enemy_tank=GameObject.Find(“Enemy_barrel”);                                                       //实例化
 
if (target == null)
 
return;
 
if (!CanSeeTarget ())
 
return;
 
// Rotate towards target
 
var targetPoint = target.position;
 
var targetRotation = Quaternion.LookRotation (targetPoint - Enemy_tank.transform.position,Vector3.up);  //求出与目标之间的角度
 
Enemy_tank.transform.rotation = Quaternion.Slerp(Enemy_tank.transform.rotation, targetRotation, Time.deltaTime * 2.0);
 
//旋转之角度与目标对齐
 
//If we are almost rotated towards target - fire one clip of ammo
 
var forward = Enemy_tank.transform.TransformDirection(Vector3.forward);
 
var targetDir =Enemy_tank.transform.position-target.position;                          //求出2者之间的距离
 
if((Vector3.Angle(forward, targetDir) <10.0)||((Vector3.Angle(forward, targetDir) >-10.0)))         //这边是自动开炮的效果
 
{
 
cun_time++;
 
if(cun_time==260)
 
{
 
var clone : Rigidbody;
 
clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);
 
clone.velocity = Enemy_tank.transform.TransformDirection (Vector3.forward*bullspeed);
 
cun_time=0;
 
}
 
}
 
if((Vector3.Distance(transform.position, target.position) < attackRange))         //发现目标而且在范围之内,开始追击目标
 
{
 
var targetPoint1 = target.position;
 
//Calculation the angle with target
 
var targetRotation1 = Quaternion.LookRotation (targetPoint1 - transform.position,Vector3.up);
 
//Rotated Itself   then make a line with target
 
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation1, Time.deltaTime * 1.0);
 
transform.Translate(Vector3.forward*0.05);
 
}
 
}
 
function CanSeeTarget (): System.Boolean                                //计算是否被发现
 
{
 
if (Vector3.Distance(transform.position, target.position) > attackRange) return false;
 
var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit))
 
return hit.transform == target; return false;
 
}

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

阅读数 531

功能:1,坦克自动旋转炮管,自动瞄准2,在坦克的射程之内自动开炮3,坦克自动向你追击脚本:[javascript]viewplaincopyvar Enemy_tank: GameObject; //定义坦克  var firepoint: Transform; //定义开火点  var bullet: Rigidbody; //定义子弹  var 

博文 来自: aa1287537617

Unity3D学习之_AI人工智能(坦克模型自动寻怪_自动追击)

阅读数 869

今天发布一篇关于但可大战的AI人工只能设置。实现了坦克的自动寻怪和自动追击功能。该文章出自【狗刨学习网】:http://www.gopedu.com/varEnemy_tank:GameObject;//定义坦克varfirepoint:Transform;//定义开火点varbullet:Rigidbody;//定义子弹v

博文 来自: Carl180

Unity3D基础论-Shader

阅读数 22

shader着色器(Shader)是用来实现图像渲染的,用来替代固定渲染管线的可编辑程序。其中VertexShader主要负责顶点的几何关系等的运算,PixelShader主要负责片源颜色等的计算。着色器替代了传统的固定渲染管线,可以实现3D图形学计算中的相关计算,由于其可编辑性,可以实现各种各样的图像效果而不用受显卡的固定渲染管线限制...

博文 来自: weixin_34248023

Unity3D 自动寻径

阅读数 824

欢迎来到unity学习、unity培训、unity企业培训教育专区,这里有很多U3D资源、U3D培训视频,我们致力于打造业内unity3d培训、学习第一品牌。    众所周知,自动寻路是所有游戏的一个难点,属于AI(人工智能)的范畴。一个游戏的AI的设计是否足够完美,可能决定了这个游戏的命运。然而自动寻路就是AI中的一个十分重要的分支,其算法异常复杂。然而unity3d中提供

博文 来自: C1805217287

unity3d 自动寻路算法

阅读数 4116

Notethatthiscodehasbeenupdated.Ihavean updatedblogpost detailingwhatchanged.Thefullsourcecodeisavailableathttps://github.com/bgrins/javascript-astarViewtheonlinedemoI

博文 来自: fzhlee
没有更多推荐了,返回首页