I’d like to create a way for my enemies to walk from waypoint to waypoint. There’s been a lot discussion about this in the forum. I have some code that does this.
my question has more to do with placing the waypoints in the level, then connecting them to the enemy.
My current setup I have is a prefab with the following script:
function OnDrawGizmos ()
{// Draws waypoint icon
Gizmos.DrawIcon (transform.position, "waypoint.png");
}
This lets me drag the waypoints around in the level, and see them in Scene view even when not selected.
In my enemy code, I have have this declared:
var waypoints : GameObject[];
and then for each enemy I add, I set up the waypoints manually in this array.
The problem with this adding the waypoints in the list. I have to drag the waypoint GOs into the inspector and since they sort alphabetically, it requires a lot of renaming and setup that is prone to error.
Anyone have a better way? I still want to be able to set them visually in the Scene editor.
For your current problem: One way of dealing with it could be to write an editor script which spawns an instance of any selected prefab, searches the scene for GOs with the same name and adds 1/2/3… to the name if found.
I was thinking of doing a scan. How do you scan all gameobjects in a scene. I assumed there would be a scene class or something that one could get all the GOs in the scene from, but I’ve not seen it.
The green waypoints are prefab’ed gameobjects with a script attached to. The prefab has a tag “Waypoint” so that I can select them via the gameobject.Findxx functions. Each waypoint has a slot for the next waypoint so I can drag the next waypoint via the inspector into the slot and automatically get a link to the next one. Very easy solution, but works great!
I further have an editor script that auto aligns the waypoints to the ground. Although a bit specific to the race game I develop, but maybe helps.