Unity随机Prefab,自动前往某点处理

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

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

C# 文件名称为RadomAPoint.cs

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



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

特别简单的代码:

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


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

代码:

[csharp] view plain copy
 
 print?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class BoxCollisionDestory : MonoBehaviour   
  5. {  
  6.     public string tagName;  
  7.   
  8.     // Use this for initialization  
  9.     void Start () {  
  10.       
  11.     }  
  12.       
  13.     // Update is called once per frame  
  14.     void Update () {  
  15.       
  16.     }  
  17.   
  18.     void OnTriggerEnter(Collider other)  
  19.     {  
  20.         if (other.gameObject.tag == tagName)  
  21.         {  
  22.             GameObject.Destroy(other.gameObject);  
  23.         }  
  24.   
  25.     }  
  26. }  



四,说明

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

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

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

原文地址:https://www.cnblogs.com/chenliyang/p/6558512.html