Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by SBull · Sep 17, 2015 at 07:33 PM · javascriptchildhierarchytransform.rotationassign-variable

Getting the rotation of an object from another object within a prefab.

I'm not really even sure how to go about asking this, but here goes. My overall goal is to make a character's eyes move by adjusting the mainTextureOffset by using the rotation of some bones in the character's eyes as the x and y values.

So I am trying to assign a transform variable on a script that I have attached to a character's eyes. Problem is that the character is created from a prefab on start, so I can't simply drop the object in the transform slot. I need to assign the transform through scripting. The eye bone is in the same object as the eye with the texture, but down a different branch of the hierarchy. So how would I access the rotation transform for the eye bone?

Here is my script so far. It works as I need it to with the exception of assigning that transform variable.

 var eyeBone : Transform;
 
 function Start ()
 {
     eyeBone = //Not sure what I need here.
 }
 
 function Update ()
 {
     var eyeBoneAngleX = eyeBone.transform.localEulerAngles.x;
     var eyeBoneAngleY = eyeBone.transform.localEulerAngles.y;
 
     renderer.material.mainTextureOffset = Vector2(eyeBoneAngleX,eyeBoneAngleY);
 }
Comment
Add comment · Show 2
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 holliebuckets ♦♦ · Sep 17, 2015 at 09:25 PM 0
Share

quick question: do you use

 GameObject characterFromPrefab = Instantiate(prefab, pos, rot) as GameObject

or just Instantiate? :)

avatar image SBull · Sep 17, 2015 at 10:08 PM 0
Share

I use:

 for (var i = 0; i < numberOfCharactersToCreate; i++) 
 { 
 characters[i] = Instantiate(Resources.Load("Prefabs/prefab_character")) as GameObject;
 }



3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SBull · Sep 18, 2015 at 05:59 PM

I have been digging through forums and manuals for the past few hours and finally figured it out. Runalotski, you were very close with

eyeBone = transform.Find("Spine1/Spine2/Neck/Head/Eye).transform;

I tried this early on, but it still wasn't working right, because it needs to start from the root. So I finally learned I needed to add in "root" to let it know that I wanted it to start searching from the top of this object's hierarchy.

 eyeBone = transform.root.Find("Spine1/Spine2/Neck/Head/Eye").transform;

This finally gave me the result I was looking for.

Comment
Add comment · 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
1

Answer by Runalotski · Sep 18, 2015 at 05:39 PM

First try making an instance of the object in your scene and then just drag the child object "Eyes" into the public transfrom then at the top of the Inspector there is an apply button click that and this reference will be saved in the prefab.

or

on line five where you wrote

  eyeBone = //Not sure what I need here.

you can use something similar to

  eyeBone = transform.Find("eye").transform

now i dont know what the path is to your eye bone but you may need to path from the object with your script to it like such

  eyeBone = transfrom.Find("Spine1/Spine2/Neck/Head/Eye").transfrom

you could also make a tag called eye and tag the bone with it then you can use

  eyeBone = transform.FindObjectWithTag("eye");
Comment
Add comment · 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
0

Answer by vashtriguner · Sep 19, 2015 at 03:30 AM

You could just name the child object something like "Eyes" or something then do a transform.root.FindChild("Eyes");

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding an offset to an instantiated objects rotation 0 Answers

How do I find a child object in a hierarchy of children? 1 Answer

How can I access the 2nd tier of children on a prefab in the Project Folder Window? 4 Answers

Placing GameObject as Child in Hierarchy Changes Its Position 1 Answer

When i click an object, Hierachy does not expand its parents, so i must look for it manually :( Help !? 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