zoukankan      html  css  js  c++  java
  • 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,否则他们的碰撞不起作用。

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

  • 相关阅读:
    派生类的构造函数
    继承和派生
    自增自减运算符的重载(强制类型转换运算符重载)
    流插入和流提取运算符的重载
    动态数组类的设计
    函数的返回值
    赋值运算符的重载
    运算符重载
    常量对象函数引用和参数传递
    理解ASP.NET MVC的路由系统
  • 原文地址:https://www.cnblogs.com/chenliyang/p/6558512.html
Copyright © 2011-2022 走看看