I’m working on a semi-stealth fps and adding an enemy that patrols back and forth. I’m trying to figure out how to make empties for the end points of a path (using pathfinding) that can be parented to the enemy prefab and moved in the editor for easy plug’n’play functionality. thinking about this, I realized the points would move with the enemy during run time, rendering this completely useless. is there a way to make a child keep it’s global transform? I found this line of code
child.transform.SetParent(newParent, false);
which sets it so the child keeps its transform, and that might work, but I don’t want to go to the hassle of figuring out how to make sure it always parents to the right enemy. is there a box I can check in the editor or a similar line of code but for preexisting parents, anything?
make the paths be the paths (with their waypoints), and make the enemies use a path
So … scene structure like so:
EverythingElseInYourLevel
AllPaths
Path1
waypoint1
waypoint2
waypoint3
Path2
waypoint1
waypoint2
waypoint3```
Then tell the enemy which path to use, no parenting relationship.
In fact, that's how I did these handy waypoing gizmos... they even centrally report themselves to the Waypoint mangler!
https://gist.github.com/kurtdekker/c2246d3780b93fe4e023695eb947e85d
You can write other retriever functions in the waypoint manager if you need the paths grouped or obtained by other means such choosing one called "Path2" for instance.
If each enemy has it’s own path and you wanted to make a prefab of that, another alternative would be to make an empty object and make the enemy and the path both children of that empty object- so that way the enemy and the path are both in the same prefab, but not parented to each other in any way.
Edit: By the way, it might sounds like I’m pointing out the obvious here, but for the longest time I had it stuck in my head that 1 prefab = 1 thing. For some reason it only recently occurred to me you could use a prefab to store a whole collection of things in different places and that it might be useful to do so in some situations. Maybe it’s just me.