• 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 Murfle · Jun 07, 2017 at 06:51 AM · rotationgameobjectspritepositionchild object

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);           
 }
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

alt text

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class WorldOffsetApply : MonoBehaviour 
 {
     public Vector3 worldOffset;
 
     public void LateUpdate()
     {
         transform.position = transform.parent.position + worldOffset;
     }
 }
 



worldoffsetexample.png (89.5 kB)
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 Woltus · Jun 09, 2017 at 09:02 PM

You can do something like this:

alt text

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


box.png (22.5 kB)
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
0

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

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Object's sprite and collider are not at same position as transform 1 Answer

postion scale and rotation greyed out and not working 1 Answer

Rotating a sprite toward the mouse position on instantiation. 0 Answers

Unity OnMouseDrag too fast for Update function 0 Answers

Cannot rotate GameObject from C# Animator attached 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges