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 /
avatar image
2
Question by revolute · Aug 20, 2014 at 07:37 AM · transformparent and child

Altering object's position without changing its childrens position?

A parent object needs to be moved without having its children changed. Anyway to do so without changing every single childrens position everytime the parent moves?

Comment
Add comment · Show 3
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 Kiwasi · Aug 20, 2014 at 09:42 AM 0
Share

Why?

The parent-child relationship is primarily to keep positions relative. If you don't want this movement, then you probably don't want a parent-child relationship

avatar image revolute · Aug 20, 2014 at 01:52 PM 0
Share

I have parent-child relationship for easier scripting... I'm expecting more than 1000 units to be generated and divided into squads, thus having them laying all around makes no sense since there would be extra useless calculations as to maintain the squads consistency. Ever played Total War games? If you did, you may get the idea though I'm reducing squad size but still, if every single unit had to calculate (find) its relative distance to hostile object in the scene, I doubt I would be making a mobile game. Furthermore, I wonder how heavy the game would be with Unity Engine being a behemoth base to run on...

avatar image revolute · Aug 20, 2014 at 02:49 PM 0
Share

Basically I need to move parent but not children position is due to the fact that

1) I'm making a squad based rts (warhammer 40k, Total War type.. You get the idea)

2) $$anonymous$$obile rigs are very heavy, with Android running heavily on background and using Unity to make the game, it is heavy enough that I dont want extra O(n^2) case per frame to check and set target. (Atleast individual units should not move around even though it has nothing to do meaning that I need to check other members of the squad to see if they can move on...) while having extra n cases whereas having triggers per unit to check valid attack range. I doubt I would get 60 on PC and lucky if I get 20 on mobiles...

3) the game I'm trying to make is a replica of another game ( not TW or W40k, just some weird game) which I'm guessing that has both collider to check if one squad collides with other squads and moves based on the coord from searching other squad to attack. (yes, this causes weird issues of units going to nowhere because that was where its target was and I'm aware and I want to make it so)

I can calculate a position of the squad using coord of children units rather than actually moving parent object so that the game works well but I was hoping for a better solution like actually moving parent object in the direction synchronized with children rigid body force so that I can get a reliable yet quick reference to squad position.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Andres-Fernandez · Aug 20, 2014 at 07:47 AM

  1. Temporarily reparent children to another object.

  2. Move parent

  3. Reparent children to parent

Comment
Add comment · Show 4 · 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 revolute · Aug 20, 2014 at 07:50 AM 0
Share

What happens if the parent moves via addforce or just every fixedupdate? I dont think it is wise to do so..

avatar image Andres-Fernandez · Aug 20, 2014 at 07:55 AM 0
Share

If you want to move parent but not children, why have the children parented to the parent? If it's just for some code like BroadCast$$anonymous$$essage, you can just create references to the children in the parent's code and send them messages.

[edit] If parent moves within fixed update reparenting would work... Not with the addforce tho...

avatar image revolute · Aug 20, 2014 at 01:48 PM 0
Share

Basically I have a squad of units. I can't make a single unit to be its leader as it can die(destroyed object) so I need to keep them controlled under an empty object for easy retaining and generation as well as other stuff; in this case I rely on the squad object position to set as target. Don't ask me why I chose this kind of movement options rather than just moving towards another squad while checking targets coordinates. I happen to be trying to make a full replica of another game.

avatar image Cherno · Aug 20, 2014 at 02:53 PM 1
Share

Ins$$anonymous$$d of a child/parent hierarchy, you might have it easier with a simple custom class that stores a leader gameObject and a list for the squaddies, and then use these references to command units.

avatar image
0

Answer by Cherno · Aug 20, 2014 at 01:50 PM

While the parent object is moving, you also make all children objects Vector3.Lerp towards the original position, which you saved before moving in a Vector3 variable.

Comment
Add comment · 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 revolute · Aug 20, 2014 at 02:08 PM 0
Share

I'll try that and see how it turns out.

avatar image MichaelBB · Jul 15, 2020 at 09:28 AM 0
Share

Did you managed to hold the childs in place?

avatar image
0

Answer by joergzdarsky · May 28, 2016 at 09:17 AM

I'd like to add a request for this possibility (transform / position parents but not the children).

Here is my usecase: I am using the (commonly used) "Floating Origin" technique for my space game. So, I generally keep my camera close to the origin (0,0,0) for precision reasons. When the camera has moved to a certain distance, I reset the camera to the origin (0,0,0) and move all objects around for the same distance -> Floating Origin.

In that case I have to move a lot of object at once. Extreme situations where I have to move everything each frame is not unlikely. Thus a highly optimized strategy is required for performance reasons.

The way I do it now is to traverse through the children of an object (which is an empty "Space"-Object) and move each object individually by the same distance. The corresponding script (which realized Floating Origin) is attached to the camera:

      [RequireComponent(typeof(Camera))]
      public class FloatingOriginByParent : MonoBehaviour
      {
          public float threshold = 6000.0f;
          public GameObject parentGameObject; // GameObject where all child objects are to be shifted
          void LateUpdate()
          {
              Vector3 cameraPosition = gameObject.transform.position;
      
              if (cameraPosition.magnitude > threshold)
              {
                  foreach (Transform child in parentGameObject.transform)
                  {
                      child.transform.position -= cameraPosition;
                  }
                  gameObject.transform.position = new Vector3(0,0,0);
              }
          }
      }

Thats the best you can do in Unity AFAIK however its still not optimal.

The above results in thousands of single shifts including a large loop, each frame. It would be performance-wise much better if I could group all objects under a "Drag" or "Anchor"-like empty gameobject, that is once shifted (including the children) for the required distance, and then reset to the origin (without the children). Doing that manually is even worse than the above code. However, shifting lots of objects by its parent is faster, but to realize Floating Origin with that you need a function to independently reset the parent.

So an additional function to transform position a parent gameobject without the children would be the key here.

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

6 People are following this question.

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

Related Questions

Instantiated object's transform changes according to parent object's rotation 1 Answer

How to rotate a child where its parent has another Z rotation? | Issue when rotating towards the mouse after it goes on the other side of the player 0 Answers

Picking up object with transform.parent does not work 1 Answer

How to set a Transform variable with code (C#) 3 Answers

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