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 /
  • Help Room /
avatar image
0
Question by sean244 · Dec 02, 2017 at 11:29 PM · 2d2d game2d-gameplay2d platformer

How to stop an object from following its parent?

I have an enemy that's patrolling an area. That area is defined by two gameobjects that are just transform positions, one called 'start' and another called 'end'. Both are children of the enemy, that way, every time I create a new enemy, they each have their own 'start' and 'end'. It just looks a lot cleaner in the hierarchy. However, they both follow their parent, which they're not suppose to do. Is they're any workaround for this?

alt text

untitled-2.jpg (14.5 kB)
Comment
Add comment · Show 8
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 Bonfire-Boy · Dec 02, 2017 at 11:54 PM 0
Share

Well, you could add code (probably to LateUpdate) that resets those GameObjects' positions every frame. But to me it looks like you don't want those transforms to be parented to the turtle.

If it's really important to you that each turtle is grouped with its start and end transforms in the hierarchy, you could put the 3 objects (turtle, start, end) at the same level, inside a bespoke container object. That way the 3 things would move independently while still having them grouped.

avatar image Owen-Reynolds Bonfire-Boy · Dec 03, 2017 at 12:27 AM 1
Share

To rephrase, make empty "turtleHolder" with children "turtle," "start" and "end." I've successfully used that trick. It's a cheap way to spawn, move and destroy them all at once. And almost as easy to find your start&end (parent.Find("start").)

avatar image sean244 Owen-Reynolds · Dec 03, 2017 at 01:57 AM 0
Share

I'll try that. Thanks.

avatar image sean244 Bonfire-Boy · Dec 03, 2017 at 01:57 AM 0
Share

Not sure what you mean by 'bespoke'.

avatar image Owen-Reynolds sean244 · Dec 03, 2017 at 02:30 AM 0
Share

Probably the dictionary definition. When you get to a device with internet access you can look it up :-)

Show more comments
avatar image unity_dI4RiuEqe3sX5A · Sep 17, 2021 at 10:51 AM 0
Share

Can we please get a way to preserve our hierarchical structure and decouple parent-children from movement? it's really useful when objects stay associated even when they move away from each other. I know you can do it with a wrapper and either use a mediating script in the parent, or else traverse the hierarchy to it's named sibling or script name, but those are all very messy. I want to be able to just 'drop' things on the ground that belong to the parent entity sometimes. Persistent AOE for instance, or really any collision based attack where the velocity is not high enough for the movement of it's origin to be irrelevant.

avatar image Bonfire-Boy unity_dI4RiuEqe3sX5A · Sep 17, 2021 at 11:34 AM 0
Share

Not quite sure what you're asking for here, but for exposure I suggest it would be better to post a new question for it rather than add the as an answer to this question.


The way I see it, the hierarchy view is just a view of the scene/prefab showing the parent/child relationships, a child being an object that moves with its parent.


If you want to create other kinds of relationships between objects you can, but they'll be the specific relationships that you need (you say "stay associated" as if it's obvious what that means, but it isn't) - so it seems reasonable for you to have to implement them yourself.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by NinjaISV · Dec 03, 2017 at 09:36 PM

You could either set the children's parents to null as soon as the game starts (this would be the best on performance, and it will still look clean and organized while in edit mode).

 // Make sure you assign the children in the inspector here.
 public Transform[] children;
 
 private void Awake () {
     // Sets each child's parent to null while keeping their world position.
     foreach (Transform child in children) {
         child.SetParent(null, true);
     }
 }


Or you could store their positions in a list and constantly update them in FixedUpdate. (Note: this is VERY bad on performance as opposed to the other method due to the constant matrix calculations.)

 // Make sure you assign the children in the inspector here.
 public Transform[] children;
 private List<Vector3> childrenPositions = new List<Vector3>();
 
 private void Awake () {
     // Sets each child's parent to null while keeping their world position.
     foreach (Transform child in children) {
         childrenPositions.Add(child.position);
     }
 }
 
 private void FixedUpdate () {
     for (int i = 0; i < children.Length; i++ ) {
         // Ensures that the child is not null and that the index is not out of bounds.
         if (children[i] != null && i < childrenPositions.Count) {
             children[i].position = childrenPositions[i];
         }
     }
 }


And lastly, you could save the world positions at the start of the game, and reference those:

 // Make sure you assign the points in the inspector here.
 public Transform start;
 public Transform end;
 private Vector3 startPosition;
 private Vector3 endPosition;
 
 private void Awake () {
     // Assigns the start and end points.
     startPosition = start.position;
     endPosition = end.position;
     // Destroys the start and end Transforms from the game to save on performance. 
     Destroy(start);
     Destroy(end);
 }


Hope this helps!

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 mefsh · Dec 02, 2017 at 11:52 PM

Not that I know of.

You could instead store the start and end positions as Vector3s in the script. https://docs.unity3d.com/ScriptReference/Vector3.html

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 celinedrules · Dec 12, 2021 at 04:13 PM

You could add a rigidbody to it and set its body type to static.

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

146 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

Related Questions

"Invalid argument"? 0 Answers

Accesing The Script Under GameObject 0 Answers

Unity Sprite renderer bug?? 0 Answers

Does anyone know how to make an object(enemy) follow a path set by the player in a 2D side scroller? 1 Answer

How do you Isolate Physics2D.OverlapArea to a single script/gameObject/Instance? 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