- Home /
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?
after fire bulletgameboj.transform.parent = null;
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?
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
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;
}
}
}
}
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.
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
Follow this Question
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