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