对与U3D  AI,看了下,自己做了小功能,以备后用啊!

一,在某区域随机产生某个对象

C# 文件名称为RadomAPoint.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. public class RadomAPoint : MonoBehaviour {
  4. public GameObject mObjArea; // 随机区域
  5. public GameObject prefabObj;    // 对象prefab
  6. public string mytag;       // 对象标签
  7. public string targetTag;    // 目标对象标签
  8. public int ObjectNumber;    // 场景中整体prefab 个数。
  9. private Bounds mbouds;
  10. private Vector3 tmp;
  11. // Use this for initialization
  12. void Start () {
  13. mbouds = mObjArea.GetComponent<Collider>().bounds;
  14. InvokeRepeating("NewPrefabInstance", 1, 5);//1秒后调用LaunchProjectile () 函数,之后每5秒调用一次
  15. }
  16. // Update is called once per frame
  17. void Update () {
  18. }
  19. void NewPrefabInstance()
  20. {
  21. GameObject[] root = GameObject.FindGameObjectsWithTag(mytag);
  22. if (root.Length <= ObjectNumber)
  23. {
  24. Vector3 randomPos = RadomVector3(mbouds.min, mbouds.max);
  25. //GameObject tmpGameObj = Resources.Load(prefabName) as GameObject;
  26. //tmpGameObj.transform.position = randomPos;
  27. Quaternion q = Quaternion.identity;
  28. GameObject tmpGameObj = GameObject.Instantiate(prefabObj, randomPos, q) as GameObject;
  29. tmpGameObj.GetComponent<AIBehaviourScript>().TargetObject = GameObject.FindWithTag(targetTag).transform;
  30. }
  31. }
  32. Vector3 RadomVector3(Vector3 min, Vector3 max)
  33. {
  34. tmp.x = Random.Range(min.x, max.x);
  35. tmp.y= Random.Range(min.y, max.y);
  36. return tmp;
  37. }
  38. }

二、自己做了个prefab,添加了自动找到目标的功能。

特别简单的代码:

  1. using UnityEngine;
  2. using System.Collections;
  3. public class AIBehaviourScript : MonoBehaviour
  4. {
  5. public Transform TargetObject = null;
  6. void Start()
  7. {
  8. if (TargetObject != null)
  9. {
  10. GetComponent<NavMeshAgent>().destination = TargetObject.position;
  11. }
  12. }
  13. void Update()
  14. {
  15. }
  16. }

三,遇到目标后,自动销毁

代码:

  1. using UnityEngine;
  2. using System.Collections;
  3. public class BoxCollisionDestory : MonoBehaviour
  4. {
  5. public string tagName;
  6. // Use this for initialization
  7. void Start () {
  8. }
  9. // Update is called once per frame
  10. void Update () {
  11. }
  12. void OnTriggerEnter(Collider other)
  13. {
  14. if (other.gameObject.tag == tagName)
  15. {
  16. GameObject.Destroy(other.gameObject);
  17. }
  18. }
  19. }

四,说明

这个过程中,要设置目标点为的属性如下:

而prefab对象也需要给它一个rigidbody,否则他们的碰撞不起作用。

基本上做了一个能随机位置产生一个对象,然后对象自动寻找目的,到达目的地的小功能!

最新文章

  1. 《与小卡特一起学Python》 Code6 注释
  2. 每日英語2013.09.09(Email常用單字)
  3. MyBatis操作指南-配置结果映射一对一,一对多,多对多(基于注解)
  4. 无法获得锁 /var/lib/dpkg/lock - open (11: 资源临时不可用)
  5. 微信 关闭手机微信内置浏览器的js
  6. 代码片段--Makefile之大型工程项目子目录Makefile的一种通用写法
  7. js两种创建对象方式
  8. Cacti以MB为单位监控流量
  9. BZOJ1677: [Usaco2005 Jan]Sumsets 求和
  10. c#中使用log4net工具记录日志
  11. Insecure default in Elasticsearch enables remote code execution
  12. mybatis简单搭建
  13. Luogu P5285 [十二省联考2019]骗分过样例
  14. 针对主机CPU idle性能情况需求脚本编写
  15. c/c++ 标准库 map multimap元素访问
  16. python 可调用对象之类实例
  17. C# Note25: .Net Core
  18. Android 自定义TextView 实现文本间距
  19. Python 学习第二章
  20. nwjs问题总结

热门文章

  1. python3打印26个英文字母
  2. 【WPF】退出应用时的提示弹窗
  3. 带命名空间的XML的dom4j应用&lt;转&gt;
  4. MDL---Material Design Lite框架推荐
  5. Java中上传文件和表单数据提交如何保持数据的一致性?
  6. Python python的输入输出
  7. Qt信号槽的一些事
  8. android tab之间滑动切换界面功能
  9. android BroadCastRecevier笔记
  10. Javascript 严格模式 strict mode(转)