Unity random prefab, automatically go to some point processing

Source: Internet
Author: User
Tags tagname

This article is written by Cartzhang, reproduced please indicate the source. All rights reserved.
Article Link: http://blog.csdn.net/cartzhang/article/details/47337029
Cartzhang


To with U3d AI, looked under, oneself did the small function, prepares for later uses Ah!


One, randomly generating an object in an area

C # filename is called RadomAPoint.cs

Using unityengine;using System.collections;public class Radomapoint:monobehaviour {public gameobject mObjArea;//random    Regional public gameobject Prefabobj;       Object prefab public string MyTag;    Object label public string Targettag;    target object label public int objectnumber;    The total number of prefab in the scene.    Private Bounds Mbouds; Private Vector3 tmp;//Use this for initializationvoid Start () {mbouds = mobjarea.getcomponent<collider> ()        . Bounds; Invokerepeating ("Newprefabinstance", 1, 5);//1 seconds after calling the Launchprojectile () function, followed by a call every 5 seconds}//Update is called once per Framevoid Update () {} void Newprefabinstance () {gameobject[] root = Gameobject.findgameobjectswithta        g (MyTag); if (root.            Length <= objectnumber) {Vector3 Randompos = RadomVector3 (Mbouds.min, Mbouds.max);            Gameobject tmpgameobj = Resources.load (prefabname) as Gameobject;            TmpGameObj.transform.position = Randompos; quaternion q = quaternion.identity;            Gameobject tmpgameobj = gameobject.instantiate (prefabobj, Randompos, q) as Gameobject; Tmpgameobj.getcomponent<aibehaviourscript> ().        TargetObject = Gameobject.findwithtag (Targettag). Transform;        }} Vector3 RadomVector3 (Vector3 min, Vector3 max) {tmp.x = Random.range (min.x, max.x);        Tmp.y= Random.range (MIN.Y, max.y);    return TMP; }}


Second, I made a prefab, added the ability to automatically find the target.

A very simple code:


Using unityengine;using System.collections;public class aibehaviourscript:monobehaviour{public    Transform TargetObject = null;    void Start ()    {        if (TargetObject! = null)        {            getcomponent<navmeshagent> (). Destination = targetobject.position;        }    }    void Update ()    {    }}

Third, after meeting the target, automatic destruction


Code:

Using unityengine;using System.collections;public class Boxcollisiondestory:monobehaviour {public    string tagName ;//Use this for initializationvoid Start () {}//Update is called once per framevoid update () {}    void Ontriggerenter (Collider Other)    {        if (Other.gameObject.tag = = TagName)        {            Gameobject.destroy (other.gameobject);}}    }


Four, explain

In this procedure, the properties to set the target point are as follows:



The prefab object also needs to give it a rigidbody, otherwise their collision does not work.



Basically do a random position to produce an object, then object automatically find the purpose, to reach the destination of the small function!


---------

If you have any questions, please feel free to contact us!

Thank you so much!!





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Unity random prefab, automatically go to some point processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.