找回密码
 立即注册
楼主: 灵之舞

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

[复制链接]
发表于 2013-1-7 19:49 | 显示全部楼层 |阅读模式
  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. 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. }
复制代码
发表于 2013-5-23 13:46 | 显示全部楼层

不错 不错 不错{:soso__3922851084632044791_6:}
发表于 2013-5-23 14:58 | 显示全部楼层
非常不错哦,支持一下!

点评

回回更健康  详情 回复 发表于 2013-6-16 17:00
发表于 2013-6-16 17:00 | 显示全部楼层
zypbluesky 发表于 2013-5-23 14:58
非常不错哦,支持一下!

回回更健康
发表于 2015-6-11 15:38 | 显示全部楼层
好像很厉害
发表于 2017-2-9 20:11 | 显示全部楼层
楼主是超人
发表于 2017-2-9 19:34 | 显示全部楼层
好帖就是要顶
发表于 2017-2-9 19:42 | 显示全部楼层
顶顶多好
发表于 2017-2-9 19:32 | 显示全部楼层
难得一见的好帖
发表于 2017-2-9 19:16 | 显示全部楼层
很好哦
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-22 13:14 , Processed in 0.090629 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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