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 ShazGeek · May 06, 2019 at 04:03 PM · 2dtransformpositiondistancemove an object

Moving child and parent

hi,

I'm stuck on how I can get a child to move but keeping the parent at the same distance once its move to the child, so if the child is at 0,0,0 and the parent distance is 9,9,0, but I move the child to 100,100,0. I want the parent to be at 109,109,0 when I move it to the child's position. basically, I want the distance kept the same where ever I move the child.

any help would be highly appreciated.

Comment
Add comment · Show 11
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 Magso · May 06, 2019 at 05:25 PM 1
Share

The child is local to the parent not vise versa, can you switch them around so the parent is the child and the child is the parent? If not you would have to get the position where you want the child, position the parent there, add the offset to the parent and take away the offset locally from the child.

avatar image Casiell Magso · May 06, 2019 at 09:45 PM 1
Share

Or just get the offset between child position and final child position and add it to the parent.

 void $$anonymous$$ove(Vector3 finalPosition)
 {
     var offset = finalPosition - child.Position;
     parent.Position = parent.Position + offset;
 }
avatar image K-Anator Casiell · May 06, 2019 at 10:00 PM 0
Share

But would that not take the child with it, thus making the distance between the two objects still too far apart? It's hard to tell exactly what Shaz is looking for without more context, but if they want the parent to remain the same amount of distance away from the child, it's the parent that needs to be moved, not the child. Which, pending that the script doing the moving is on the child, could be done with something like:

     public Vector3 parentTarget;
     GameObject ParentObject;
     void Start()
     {
         ParentObject = GetComponentInParent<GameObject>();
     }
 
     private void Update()
     {
         ParentObject.transform.position = parentTarget;
     }

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by darkStar27 · May 07, 2019 at 05:48 AM

One thing you can do is save your position difference to a variable, and add it to your parent whenever you move you child.

 /*
 Script is attached to parent
 */
 
 public Vector3 parentOffset;
 
 
 void Awake(){
      parentOffset = GetComponentInChildren<Transform>().localPosition;
 }
 
 void Update{
      // If targetPosition is varible that contains the position that is to be moved at then
      // move you child at targetPosition
      // move you parent at targetPosition+parentOffset;
 }
 
 // just remember targetPosition is also a Vector3.

Hope this helps.

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 A_Savvidis · May 07, 2019 at 09:12 AM 0
Share

Please tell us, is the child moving by scripting? By physics/collisions etc?

Why don't you just swap the child <-> parent by scripting so you can control that automatically?

something like this perhaps.

avatar image ShazGeek A_Savvidis · May 09, 2019 at 12:57 PM 0
Share

what I am doing is using a start point as a child and trying to get the parent offset to the child and then moving them both to a different position but keeping the offset. I have it so far working with some of the items with remember local offset and then moving it but I have two items not working.

avatar image ShazGeek · May 07, 2019 at 02:38 PM 0
Share

this helped me understand what I done wrong thank you

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

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

Related Questions

Move with Rigidbody2D in the Z axis 3 Answers

how do i fix my script in the content below? 0 Answers

Reposition to minimum distance from target 2 Answers

How do I make my camera follow the player without rotating in 2D? 2 Answers

Two gameobjects with the same transform 1 Answer


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