- Home /
How can I collect the positions of my waypoint objects which mark out a path or route?
Hi guys, im quite new to unity so this is probably something quite simple. Im attempting to use this code here:
http://forum.unity3d.com//files/smoothcurve_208.js
to get my object to follow the curve - all is working fine.
However, instead of getting the 'waypoint' position info via the inspector this way:
var points : Vector3[];
( where i have to type the x,y,z into the inspector )
Is there any way of getting the x,y,z info of the waypoint objects i have added to the scene and then having that populate the Vector3 array in the code??
preferably using this way:
var waypoints : Transform[];
I think its all the builtin array stuff thats throwing me.
Thanks
Danny
p.s. should i delete this question from the forum as i have repeated it there ?
Answer by duck · Apr 21, 2010 at 03:17 PM
Yes I do this often (for example to collect the waypoints which mark out a racetrack).
If your waypoints are numbered, you can search for them by name like this:
var markersList = new ArrayList(); var markerNum = 0; var searching = true;
while (searching) { markerNum++; var markerName = "routemarker" + markerNum; var markerObject = GameObject.Find(markerName);
if (markerObject != null)
{
markersList.Add(markerObject.transform.position);
}
else
{
searching = false;
}
}
Debug.Log("Found " + markersList.Count + " route markers");
If you need to, you can then convert the ArrayList to a built-in array like this:
var markers = markersList.ToArray(typeof(Vector3));
If your route markers are numbered with zero padding (i.e. 001, 002 etc), you can find their names like this:
var markerName = "routemarker" + markerNum.ToString().PadLeft(2,'0');
I think it's best to numerically name your markers, however if your markers aren't numerically named, you could get them all by assigning a custom tag to them all, and then using FindGameObjectsWithTag, however I'm not 100% sure that they will always be collected in the correct order.
Answer by Herman-Tulleken · Jul 16, 2010 at 01:05 PM
If you use the array of Transforms as you suggest, you can conveniently drag nodes into the array, and copy their positions with the following snippet:
var waypoints : Transform[]; // for inspector private var waypointVectors : Vector3[]; //for use in script
function Start () //assumes nodes will not change wile the game is running { waypointVectors = new Array(waypoints.length);
for(i = 0; i < waypoints.length; i++)
{
waypointVectors[i] = waypoints[i].position;
}
}