- Home /
How to rotate/position the child object independently of the parent?
I'm attempting to stack slightly offset sprites to achieve a Semi-3D look for my topdown 2D game.
I don't quite know how to phrase this, but If a parent rotates, then a position-offset child will "orbit" said parent.
I don't want this to be the case. I want to have the child always have the same FACING as the parent while also KEEPING the position same position/offset relative to the parent.
Video of the rotation: https://i.gyazo.com/c215a09fb1a28d85b3f146270c34c093.mp4
My current solution is a quick mockup that de-parents the child sprite container named "*'s UnderSprites", but this makes the hierarchy messy, and the individual objects impossible to manage properly. Here's the code:
void Start () {
xParentGO = this.transform.parent.gameObject;
if (this.name == "UnderSprites") this.name = this.transform.parent.name + "'s UnderSprites";
this.transform.parent = null;
childGO1 = transform.GetChild(0) ;
childGO2 = transform.GetChild(1) ;
}
void Update ()
{ this.transform.rotation = xParentGO.transform.rotation;
childGO1.transform.position = xParentGO.transform.position + new Vector3(obj1xOffset, obj1yOffset, obj1zOffset);
childGO2.transform.position = xParentGO.transform.position + new Vector3(obj2xOffset, obj2yOffset, obj2zOffset);
}
Answer by NoBrainer-David · Jun 07, 2017 at 08:15 AM
Chaining the transformations is kind of inherent in the parent-child relationship. So if you want to keep that relationship for tidiness in the hierarchy, you will constantly fight against the engine.
I would suggest you use an empty object that groups the main-sprite and the under-sprites together, while keeping the transforms of each independent. Then, you can use a simple script that adds a world-coordinate offset to its parent position for its own position.
Here is a picture of the setup and the code for the WorldOffsetApply script. Note the [ExecuteInEditMode] attribute. This way, you can test it without hitting play.
using UnityEngine;
[ExecuteInEditMode]
public class WorldOffsetApply : MonoBehaviour
{
public Vector3 worldOffset;
public void LateUpdate()
{
transform.position = transform.parent.position + worldOffset;
}
}
Answer by Woltus · Jun 09, 2017 at 09:02 PM
You can do something like this:
Parent and child are just a sprites. Box Rotation script:
public class BoxRotate : MonoBehaviour {
Transform[] transformsToRotate;
[SerializeField]float rotationSpeed = 30f;
// Use this for initialization
void Start () {
transformsToRotate = GetComponentsInChildren<Transform>();
}
// Update is called once per frame
void Update () {
for (int i = 1; i < transformsToRotate.Length; i++)
{
transformsToRotate[i].Rotate(Vector3.forward * Time.deltaTime * rotationSpeed);
}
}
}
My effect:
http://i10.photobucket.com/albums/a146/woltus/box_zpseccnna8c.gif
Answer by Alp21 · Sep 24, 2020 at 08:24 AM
I have a partially relevant question. I also have a player under an empty gameobject and the problem is the network. I am using mirror. I put the network identity and transform in the empty gameobject but the movement of the player is not being transferred to other game screen also the position of the empty game object is not changing with the child objects thank you. @NoBrainer-David
Your answer
Follow this Question
Related Questions
Scrolling Sprite Object 0 Answers
How To Move GameObjects In Fixed Path? 1 Answer
How can I defer the recalculation of child transforms whilst modifying the parent transform? 0 Answers
Position and rotation of a game object acting wired 0 Answers
Instantiate a GameObject at a position specific to an element that was found in a text 1 Answer