In Unity it’s quite common to need to mark specific transform children as attachment points. You have a character, and want to attach a gun to his hand. How do you find the hand? GameObject.Find is too brittle (some artist changes the name of a gameObject in the file, and everything breaks).
What we do is that we add a component to a game object to serve as an extended tag.
public class Marker<T> : MonoBehaviour where T : MonoBehaviour { public static Transform Get (Transform rootToSearchFrom) { T marker = rootToSearchFrom.GetComponentInChildren<T> (); if (!marker) { Debug.LogError ("Unable to find " + typeof (T) + " in " + rootToSearchFrom.name, rootToSearchFrom); return rootToSearchFrom; } return marker.transform; } }
This does a search inside the local hierarchy, has some error checking and defaults to the root you’re searching from (so you don’t get nullrefs in other parts of the code)
(To see more about the templating trick I use, see my previous post)
To make your weapon hand, you’d do:
public class MkrWeaponHand : Marker<MkrWeaponHand> {}
Inside our main character, we can now do MkrWeaponHand.Get (transform)
to find the right transform to attach a gun at.
Imagine you have a gun; it has a some graphics (so there’s a graphics model involved). However, you want flames and bullets to be spawned from a specific point inside the model.
public class MkrTurretOpening : Marker<MkrTurretOpening> {}
So we do MkrTurretOpening.Get (transform)
to get the transform.
Notes:
That’s it for now. And I have some graphics tricks, I’m dying to show you. That just takes a long time to write, so in the meantime you get this 😉
Add Your Comment