- Home /
add waypoint to transform[]
Hey, Im trying to add a new gameObject (empty) to a transform array thing (Im not to knowledgeable on how arrays work/what they are) and have a bot patrol between these empties, heres the code;
var WayPoint : Transform[];
private var CurrentWayPoint : int;
var Speed : float = 20.0;
Awake() VVVVVVV
WayPoint[0] = transform;
Update() VVVVVVV
if(CurrentWayPoint < WayPoint.length)
{
var target : Vector2 = WayPoint[CurrentWayPoint].position;
var moveDirection : Vector2 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1)
{
CurrentWayPoint++;
}
else
{
velocity = moveDirection.normalized * Speed;
}
}
else
{
if( loop )
{
CurrentWayPoint = 0;
}
else
{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
Now, this is the new waypoint;
var NewWayPoint = new GameObject("EnemyWayPoint3");
Answer by by0log1c · Apr 24, 2011 at 05:02 PM
I'm not 100% sure what you need but I figure you're asking how to add an element to a built-in array of predefined size. I wrote a simple example not long ago, here it is:
var myWPs:Transform[] = new Transform[9];
function AddWP(wp:Transform){
var buffer:Transform[] = myWPs;
myWPs = new Transform[buffer.length+1];
for(i=0;i<buffer.length;i++){ myWPs[i] = buffer[i]; }
myWPs[myWPs.length-1] = wp;
}
Right but why use a built-in array anyway? It's not like this is a preformance critical piece of code. I'd just use new arr : Array = new Array (); and then arr = container.GetComponentsInChildren(Transform).
But maybe I'm just too lazy ^.^
Answer by Joshua · Apr 24, 2011 at 05:00 PM
I think this great tutorial might help you with exactly what you want. Go to step 5.
Now as to adding waypoint to an array. I assume that in your hierarchy view you have one empty gameobject (at 0,0,0) called waypoints. Childed to this empty container are all your waypoints. Have all these waypoints in alphabetical order as you want them to come up. For instance waypoint1, waypoint2, etc. Or just 1,2,3...
In your script use GetComponentsInChildren
nameofyourarray = nameofthewaypointscontainer.GetComponentsInChildren(Transform);
to add the transform of the container and all of its children (the waypoints) in that order in an array. Now you'll want to remove the first transform - the one of the useless container. Use Shift
nameofyourarray.Shift();
Now your array has all of the transforms of all your waypoints in the correct order. Reference them using
nameofyourarray[numberofthewaypoint].position
Good luck!
Note that this is using a Javascript Array, which is not declared using Type[]. Therefore I believe these functions will not work on the specific setting we're discussing.
You're absolutely right that he'd have to change the part where he declares the variable into var WayPoint : Array = new Array ();
Your answer
Follow this Question
Related Questions
Keep adding targets to a list 2 Answers
FindClosestEnemy() change target? 1 Answer
Way Point Assignment 4 Answers
instantiating objects problem 1 Answer
Get all other GameObjects (not the one script is attached to) 2 Answers