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
0
Question by $$anonymous$$ · Nov 14, 2014 at 04:39 AM · rotationparent-child

Tell Object not to move with its parent

So, I have bullets, which are children of their weapon. I like it like this for organizational purposes. But I do not want the bullet to rotate with the weapon.

The easy solution is obviously to make the bullets not a child of the weapon. But is there a way I can programmatically say "Don't listen to your parent!" so that I can keep it as a child?

Comment
Add comment · Show 2
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 Mayank Ghanshala · Nov 14, 2014 at 04:56 AM 0
Share

after fire bulletgameboj.transform.parent = null;

avatar image V_IPIN · Nov 14, 2014 at 05:23 AM 0
Share

you can stop moving child by rigidbody constraints.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dragonsnare0 · Nov 14, 2014 at 05:55 AM

Not that I can think of. But if it's organization you're after why don't you have a parent for the bullets that is separate from the weapon?

Put a function in the script for that parent to spawn the bullets that the weapon script can access.

Another Idea is to have the bullets spawn into the world and have the weapon script store their targets in a array so you can destroy them or whatever through the weapon script itself.

Problem solved?

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
1

Answer by IEMatrixGuy · Nov 14, 2014 at 05:56 AM

Hi,

Practically its not really possible to not move an child if its parent moves, however a workaround for this would be to simply store each bullet in an array, on the Gun Script and not have them parented. So this way you should be able to do all necessary operations including movement if you want, without having to make them move with the gun.

Hope that helps :)

IEMatrixGuy

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
-1

Answer by Rayeloy · Feb 07, 2020 at 04:20 AM

Hello, I know is kind of late to answer this, but for anyone looking to do the same:

@IEMatrixGuy It is indeed possible to not move a child with its parent, here is the code for the script I did. Just drag and drop it on the GameObject that you don't want to move with its parent:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 [ExecuteAlways]
 public class DontMoveWithParent : MonoBehaviour
 {
     Vector3 savedPosition;
 
     [Tooltip("When DontMoveWithParent is on, Ctrl+Z doesn't work for movement changes on this GameObject.")]
     public bool dontMoveWithParent = true;
     bool lastDontMoveWithParent = true;
 
     Vector3 parentLastPos;
 
     private void Update()
     {
         if (transform.hasChanged && !transform.parent.hasChanged && savedPosition != transform.position)
         {
             savedPosition = transform.position;
             transform.hasChanged = false;
         }
 
         if (!lastDontMoveWithParent && dontMoveWithParent)
             savedPosition = transform.position;
 
             lastDontMoveWithParent = dontMoveWithParent;
     }
 
     private void LateUpdate()
     {
         if (dontMoveWithParent)
         {
             if (savedPosition == Vector3.zero)
             {
                 savedPosition = transform.position;
             }
 
             if (transform.parent.hasChanged)
             {
                 transform.position = savedPosition;
                 transform.parent.hasChanged = false;
             }
         }
     }
 }
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 IEMatrixGuy · Feb 07, 2020 at 09:42 AM 1
Share

Hey man,

Thanks for the update, and the script. I'd like to clarify, that 6 years ago, I was not saying that it was impossible, I was simply implying that doing it would be a bad idea. Parenting an object to another object is more than an organisational relationship - it implies that you want the two object's transforms to be linked. Writing a script to go against this relationship is kind of going against the grain of how unity is designed. It also brings a slew of performance and code quality issues with it, as this script will cause a large quantity of transform updates, which if applied to a large amount of transforms, could actually have a noticeable impact if targeting a performance limited platform like mobile. I can't think of a case where you wouldn't want a child object to follow it's parent. If it's organisation that you're after (like OP in this case), feel free to create a Bullet parent object that doesn't move, and parent all the bullets to that. I hope this clarifies my earlier statement.

avatar image HenryStrattonFW · Feb 07, 2020 at 09:48 AM 1
Share

Hey, kind of late to comment on this, but for anyone looking at this response. DO NOT DO THIS. Will this let you parent something without inheriting the movement? sure. Should you ever reasonably do this? ....No...Why?

As matrix guy said, it's just not practical, it's unnecessary overhead, and it's execute always? Look up object pooling, and have a parent that never moves, that you can child your bullets to if you want to care about clean hierarchy.

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

31 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

Related Questions

Why is a child object not rotating properly with parent? 1 Answer

Why do grouped Terrain objects move together but don't rotate as one? 1 Answer

children doesn't take parent's rotation, position or scale. 1 Answer

Having difficulty with Parent Object rotation and Child Object movement. 0 Answers

Relative transforms and parenting 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