Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
2

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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges