找回密码
 立即注册
楼主: 多米诺

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

[复制链接]
发表于 2012-6-11 23:32 | 显示全部楼层 |阅读模式
  1. <pre>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. Enemy_tank=GameObject.Find("Enemy_barrel"); //实例化
  33. if (target == null)
  34. return;
  35. if (!CanSeeTarget ())
  36. return;
  37. // Rotate towards target
  38. var targetPoint = target.position;
  39. var targetRotation = Quaternion.LookRotation (targetPoint - Enemy_tank.transform.position,Vector3.up); //求出与目标之间的角度
  40. Enemy_tank.transform.rotation = Quaternion.Slerp(Enemy_tank.transform.rotation, targetRotation, Time.deltaTime * 2.0); //旋转之角度与目标对齐
  41. //If we are almost rotated towards target - fire one clip of ammo
  42. var forward = Enemy_tank.transform.TransformDirection(Vector3.forward);
  43. var targetDir =Enemy_tank.transform.position-target.position; //求出2者之间的距离
  44. if((Vector3.Angle(forward, targetDir) <10.0)||((Vector3.Angle(forward, targetDir) >-10.0))) //这边是自动开炮的效果
  45. {
  46. cun_time++;
  47. if(cun_time==260)
  48. {
  49. var clone : Rigidbody;
  50. clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);
  51. clone.velocity = Enemy_tank.transform.TransformDirection (Vector3.forward*bullspeed);
  52. cun_time=0;
  53. }
  54. }
  55. if((Vector3.Distance(transform.position, target.position) < attackRange)) //发现目标而且在范围之内,开始追击目标
  56. {
  57. var targetPoint1 = target.position;
  58. //Calculation the angle with target
  59. var targetRotation1 = Quaternion.LookRotation (targetPoint1 - transform.position,Vector3.up);
  60. //Rotated Itself then make a line with target
  61. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation1, Time.deltaTime * 1.0);
  62. transform.Translate(Vector3.forward*0.05);
  63. }
  64. }
  65. function CanSeeTarget (): System.Boolean //计算是否被发现
  66. {
  67. if (Vector3.Distance(transform.position, target.position) > attackRange) return false;
  68. var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit))
  69. return hit.transform == target; return false;
  70. }</pre>
复制代码
发表于 2013-6-16 18:10 | 显示全部楼层
参考一下
发表于 2014-3-5 10:27 | 显示全部楼层
学习下看看{:5_427:}
发表于 2017-2-22 18:16 | 显示全部楼层
楼主是超人
发表于 2017-2-22 18:50 | 显示全部楼层
好帖就是要顶
发表于 2017-2-22 18:24 | 显示全部楼层
顶顶多好
发表于 2017-2-22 19:02 | 显示全部楼层
难得一见的好帖
发表于 2017-2-22 18:27 | 显示全部楼层
LZ真是人才
发表于 2017-3-2 17:37 | 显示全部楼层
很不错
发表于 2017-3-2 17:33 | 显示全部楼层
楼主是超人
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-21 20:46 , Processed in 0.426962 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表