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?

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 :slight_smile:

IEMatrixGuy

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?

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;
            }
        }
    }
}