Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 02, 2020 at 09:19 PM by Zentiu for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Zentiu · Mar 04, 2014 at 06:27 PM · childtransform.look at

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zentiu · Mar 04, 2014 at 07:07 PM 0
Share

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 :) )

avatar image robertbu · Mar 04, 2014 at 07:28 PM 1
Share

Your code won't work. Assu$$anonymous$$g it is a first-level child, you can do:

 redirect = transform.Find("IChooseYouGameObject");
avatar image Zentiu · Mar 04, 2014 at 07:40 PM 0
Share

I see.. thnx for helping me understand.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Parent Game Object to Camera? 2 Answers

accessing all of a colliders parents children 1 Answer

Rotate both parent and child around parent 0 Answers

Local child rotation and transform.LookAt 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges