- Home /
The question is answered, right answer was accepted
c# getting gameobject to look at its child that is an empty gameobject (only transform component)?
think my question says it all but to clarify...
my player has an empty gameobject just slightly behind him and if he collides with a wall, the player does a 180 (looks at his child gameobject's position) and walks the other way so that he doesn't clip through the walls.
It works however..
my enemy's (mobs) are not in the scene normally.. I instantiate them through whatever.. it shouldn't be a problem with my patrolling enemy's (I just make sure they don't navigate themselves through walls). my normal mobs run to arbitrary and random directions and they sometimes take them against walls and sometimes clip through them. to avoid this I want to do the same thing that I did with my player but I cant (or don't know how to ) make the mobs on child (empty gameobject) set in the inspector since each mob has its own child to look at when facing a wall. this gameobject only has a transform and I don't quite know (if its even possible) to reference that child through scripting.
if the child had a script or anything else to reference them by then the problem would be easy but this time I don't want to put useless stuff in a gameobject that has only one purpose.
so.. is it possible to get a reference of that empty gameobject and sent its transform to its parent through scripting?
Answer by robertbu · Mar 04, 2014 at 06:53 PM
Before you go to all the trouble of solving this specific problem, be aware that you can use just use Quaternion.LookRotation() and transform.rotation to turn your characters around:
transform.rotation = Quaternion.LookRotation(-transform.forward);
Or even simpler:
transform.forward = -transform.forward;
As for the specific problem you ask about, there are two easy ways to solve the problem. First you can create a variable in the parent and drag and drop the child on this variable:
var lookBehindObject : GameObject;
Linking internally will works with Prefabs. You just cannot link externally.
If you prefer to do this operation dynamically, and the game object is an immediate child, you use Transform.Find() to find that child by name. If you have a deeper nesting, you can GameObject.GetComponentsInChildren() and specify 'Transform' for the component. Then you can find the one you want from the list returned by the name of the child object.
Thnx for the answer, btw just so I can understand..
just this line of code would work for an empty gameObject? :
Transform redirect;
redirect = GetComponentInChildren<Transform>().name("IChooseYouGameObject");
and it would just grab the childs transform?
on the sidenote.. that first idea is it seems the way to go (even IF the mobs will look like they are in a pinballmachine :P nothing float speed cant fix :) )
Your code won't work. Assu$$anonymous$$g it is a first-level child, you can do:
redirect = transform.Find("IChooseYouGameObject");
Follow this Question
Related Questions
Make a simple tree 1 Answer
Parent Game Object to Camera? 2 Answers
accessing all of a colliders parents children 1 Answer