Make Parent Follow Child and Vice Versa?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveWithChild : MonoBehaviour {
[SerializeField]
private Transform follow = null;
private Vector3 originalLocalPosition;
private Quaternion originalLocalRotation;
private void Awake()
{
originalLocalPosition = follow.localPosition;
originalLocalRotation = follow.localRotation;
}
private void Update()
{
transform.position = follow.position;
//HAS TO BE IN THIS ORDER
//sort of "reverses" the quaternion so that the local rotation is 0 if it is equal to the original local rotation
follow.RotateAround(follow.position, follow.forward, -originalLocalRotation.eulerAngles.z);
follow.RotateAround(follow.position, follow.right, -originalLocalRotation.eulerAngles.x);
follow.RotateAround(follow.position, follow.up, -originalLocalRotation.eulerAngles.y);
//rotate the parent
transform.rotation = follow.rotation;
//moves the parent by the child's original offset from the parent
transform.position += -transform.right * originalLocalPosition.x;
transform.position += -transform.up * originalLocalPosition.y;
transform.position += -transform.forward * originalLocalPosition.z;
//resets local rotation, undoing step 2
follow.localRotation = originalLocalRotation;
//reset local position
follow.localPosition = originalLocalPosition;
}
}
I found this code on YouTube courtesy of Nova840. https://www.youtube.com/watch?v=NFBEgKd1mSc. It works well for my purposes accept the scale of the parent object needs to be "1". What is your opinion of this code? I should clarify that I'm trying to keep a single parent and multiple children all locked to each other's original, relative positions, indefinitely. If one child moves, the other children, as well as the parent, all move together. I attach an instance of this script to the parent for each child. Is there an easier way to accomplish this task? Thank you for your time. @aldonaletto
Answer by jayghost1983 · May 11, 2020 at 02:33 AM
One year six months, and seven days and nobody else has responded to this? I am trying to do the same thing - with a slight twist. I have an NPC human character, that when he dies I want a zombie to seem to spawn in (after a period of time, of course) - so I have a parent object "NPC" with children "Living" and "Undead" but the problem is when the living run around the parent doesn't move - so when the living dies, the Undead appear where their parent object is, and not where they died as I had planned. My fix: I simply made the zombie a child of the living object, until the living character dies, then the zombie changes its parent to the game manager object "the world" so the living object can be destroyed and the zombie object can be "SetActive(true)" as intended.
Still, I am responding to this, because I tried to use this code with just a few slight modifications, and it did not work, at all. The parent doesn't move. @BillyBobBeavis I hope your endeavor has more fortune than mine.