• 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 /
  • Help Room /
avatar image
0
Question by BillyBobBeavis · Dec 03, 2018 at 02:57 AM · positionparentchildchildrenpositioning

Make Parent Follow Child and Vice Versa?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class moveWithChild : MonoBehaviour {
 
     [SerializeField]
     private Transform follow = null;
 
 
     private Vector3 originalLocalPosition;
     private Quaternion originalLocalRotation;
 
     private void Awake()
     {
         originalLocalPosition = follow.localPosition;
         originalLocalRotation = follow.localRotation;
     }
 
     private void Update()
     {
             transform.position = follow.position;
         
             //HAS TO BE IN THIS ORDER
             //sort of "reverses" the quaternion so that the local rotation is 0 if it is equal to the original local rotation
             follow.RotateAround(follow.position, follow.forward, -originalLocalRotation.eulerAngles.z);
             follow.RotateAround(follow.position, follow.right, -originalLocalRotation.eulerAngles.x);
             follow.RotateAround(follow.position, follow.up, -originalLocalRotation.eulerAngles.y);
         
             //rotate the parent
             transform.rotation = follow.rotation;
         
             //moves the parent by the child's original offset from the parent
             transform.position += -transform.right * originalLocalPosition.x;
             transform.position += -transform.up * originalLocalPosition.y;
             transform.position += -transform.forward * originalLocalPosition.z;
         
             //resets local rotation, undoing step 2
             follow.localRotation = originalLocalRotation;
         
             //reset local position
             follow.localPosition = originalLocalPosition;
     }
 
 }

I found this code on YouTube courtesy of Nova840. https://www.youtube.com/watch?v=NFBEgKd1mSc. It works well for my purposes accept the scale of the parent object needs to be "1". What is your opinion of this code? I should clarify that I'm trying to keep a single parent and multiple children all locked to each other's original, relative positions, indefinitely. If one child moves, the other children, as well as the parent, all move together. I attach an instance of this script to the parent for each child. Is there an easier way to accomplish this task? Thank you for your time. @aldonaletto

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

· Add your reply
  • Sort: 
avatar image
0

Answer by jayghost1983 · May 11, 2020 at 02:33 AM

One year six months, and seven days and nobody else has responded to this? I am trying to do the same thing - with a slight twist. I have an NPC human character, that when he dies I want a zombie to seem to spawn in (after a period of time, of course) - so I have a parent object "NPC" with children "Living" and "Undead" but the problem is when the living run around the parent doesn't move - so when the living dies, the Undead appear where their parent object is, and not where they died as I had planned. My fix: I simply made the zombie a child of the living object, until the living character dies, then the zombie changes its parent to the game manager object "the world" so the living object can be destroyed and the zombie object can be "SetActive(true)" as intended.


Still, I am responding to this, because I tried to use this code with just a few slight modifications, and it did not work, at all. The parent doesn't move. @BillyBobBeavis I hope your endeavor has more fortune than mine.

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.

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

179 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

Related Questions

Why can't I access the public variables of the children class of TestItem in this syntax? I only have access to TestItem variables 1 Answer

Destroy one child (of same instance) after another? 0 Answers

[PUN] Change Parent's GameObject in Network 0 Answers

destroy other child objects when the parent counts more than 1 child 1 Answer

Child transform same as parents 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