• 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
Question by Stridulent · Jul 11, 2012 at 02:07 PM · rotatedistanceexplodeexpand

Finding distance from pivot point of parent obj

Hello, community!

I am currently using Unity to create some 3D representations of some tools and objects. These scenes allow the user to rotate the object, zoom in and out, and turn off/on any animations it might have.

Also, these objects are usually made up of multiple child objects that make up the innards and such. What I am looking to do is find the distance from the center of the parent object to each individual child object, and then transform.position the child objects by 2x that. The result would be to give off an "expanded view" of the inner workings.


Here is my current code in C#:

     void Start()
  {
  cameraZoom = (maxDistance + minDistance) / 2; //Set camera start distance in the middle of min and max range
  Camera.main.orthographicSize = cameraZoom;
  
  savedLocalPos = transform.localPosition;
  }
  
  void Update() 
  {
  if (!automate)
  HandleInputs();
  
  else if(automate)
  Automate();
  
  if (disassemble)
  Disassemble();
 
  else if (!disassemble)
  transform.localPosition = savedLocalPos;
  
  if(Input.GetAxis("Mouse ScrollWheel") != 0)
  cameraZoom = Mathf.Clamp(cameraZoom - Input.GetAxis("Mouse ScrollWheel") * 2, 
  minDistance, maxDistance);
     
  Camera.main.orthographicSize = cameraZoom;
  
         CalculateObjectRotation();
  
  }
  
  void HandleInputs()
  {
  if(Input.GetMouseButton(1)) 
      {
          xDeg -= Input.GetAxis("Mouse X") * speed;
          zDeg -= Input.GetAxis("Mouse Y") * speed;
      }
      
  if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
      {
  xDeg -= Input.GetAxis("Horizontal") * speed;
  zDeg -= Input.GetAxis("Vertical") * speed;
  }
  }
  
  void Automate()
  {
  xDeg -= speed * speedMod / 20;
  yDeg += speed * speedMod;
  zDeg = 0;
  }
  
  void Disassemble()
  {
  transform.localPosition = savedLocalPos * 2; 
  }
  
  void OnGUI()
  {
  GUI.skin = customSkin;
  
  if(!automate)
  {
  if(GUI.Button(new Rect(5,5,110,25), "Engage") && !automate)
  automate = true; 
  }else
  {
  if(GUI.Button(new Rect(5,5,110,25), "Disengage") && automate)
  automate = false;
  
  speedMod = GUI.HorizontalSlider(new Rect(Screen.width/2 - 150, Screen.height - 50, 300, 100), speedMod, speedModMin, speedModMax);
  GUI.Box(new Rect(Screen.width/2 - 50, Screen.height - 37, 100, 25),"Speed = " + speedMod.ToString("F2") +"x");
  }
  
  
  if(!disassemble)
  {
  if(GUI.Button(new Rect(5,35,110,25), "Disassemble") && !disassemble)
  disassemble = true; 
  }else
  {
  if(GUI.Button(new Rect(5,35,110,25), "Assemble") && disassemble)
  disassemble = false;
  } 
  
  if(GUI.Button(new Rect(5,65,110,25), "Help"))
  {
  //still working on this pop-up window
  } 
  
  if(GUI.Button(new Rect(5,95,110,25), "Reset Rotation"))
  {
  if(automate)
  {
  xDeg = 0f;
 
  } else xDeg = yDeg = zDeg = 0f;
  
  } 
  
  cameraZoom = GUI.VerticalSlider(new Rect(Screen.width - 12, 5, 100, Screen.height - 55), cameraZoom, minDistance, maxDistance);       
     }
  
  void CalculateObjectRotation()
  {
  fromRotation = transform.rotation;
         toRotation = Quaternion.Euler(yDeg,xDeg,zDeg);
         transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime  * lerpSpeed);
  }
 }
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Owen-Reynolds · Jul 11, 2012 at 07:29 PM

I didn't see the part of the code that appeared to match the question, so this is just a response to the paragraph. You can use localPosition to read and set the your local position, using the parent's local axis, from it's position.

It works the same way as changing a childed object's position in the Inspector (when you're childed, Unity shows your local-pos in the position slot.) So:

 Vector3 savedLocalPos;

 Start() { savedLocalPos = transform.localPosition; }

 // "blow up" to be twice as far from parent's center:
 transform.localPosition = savedLocalPos * 2;

If you don't always want to expand from the center, a cheap trick is to make dummy empties. For example, you want all parts of the handle to expand away from the handle's center (not the mop's center.) Child dummy "handleMount" at the handle, and child handle components to handleMount. LocalPos will now take them away/towards that.

Comment
Bunny83

People who like this

1 Show 2 · 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 Stridulent · Jul 11, 2012 at 08:54 PM 0
Share

Thanks for you response. Currently this script is attached to only the parent object. Do I need to write a separate script for the children? I've added the updated script to the original post, as of right now nothing happens.

avatar image Owen-Reynolds · Jul 12, 2012 at 03:35 PM 1
Share

To change one kid, the parent has to "reach down" with something like: transfrom.Find("handle").localPosition= ...;. To touch all kids, you'd use a foreach (an example in the Scripting reference.) But, lot of work if you have lots of kids.

Yes, it might be easier to write one more script (one total) that goes on each kid. That way they can each save their starting pos (otherwise the parent would need an array to store then all.) It might have one function: disassemble(float percent) { trans.locPos=savedPos * percent; } that the parent calls.

Then the parent could use a single SendMessage(whatever). It tells all your kids and grandkids to do whatever() -- no loops needed (disclaimer -- I haven't used SendMessage.)

avatar image

Answer by BudgeB · Jan 22, 2013 at 04:01 PM

It's good advice and I've done a similar thing to implement pivots / anchor points, but the important thing is to have one object loop through all the children and update their object. I doubt SendMessage overhead is less than just explicitly looping through the children in a recursive function (which is what I do, once per frame, and only from the "root"). I.e. you don't want every child to update its own children, just one parent do it once per frame so there is no duplicate / redundant processing.

Apologies for the snark, but advising someone they can use a SendMessage message passing when you haven't even used it yourself is bad form. I see this type of non-help ALL the time in these forums.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How to make a game charachter rotate towards you when close enough? 1 Answer

Wrong Camera axis after "zoom" to GameObject 0 Answers

how do I make a joint that fixates the objects distance but lets them rotate freely? 1 Answer

Camera rotation around player while following. 6 Answers

"Show GUITexture within distance" issue 0 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