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 theBuilder10000 · Jul 18, 2016 at 04:55 AM · transformparent-childstealth

How to change an object's parent without changing it's z position in Unity 2D.

All other methods for locking the z axis won't work. When the z-axis changes so wildly, the object tends to be moved behind the parent and other objects. This is the code on the player character that "grabs" the treasure. It works, but the treasure is hidden behind the second player because the z position changes.

 void OnCollisionEnter2D(Collision2D other){
         if (other.gameObject.tag == "Beholder") {
             Debug.Log ("Take Treasure");
             treasure.transform.position = new Vector2(gameObject.transform.position.x,gameObject.transform.position.y);
             treasure.transform.parent = gameObject.transform;
         }
         if (other.gameObject.tag == "Treasure") {
             treasure.transform.position = new Vector2(gameObject.transform.position.x,gameObject.transform.position.y);
             treasure.transform.parent = gameObject.transform;

The treasure is purple, and player 1 is blue. alt text Also sometimes the treasure sprite is squashed in relation to the player sprite. Only sometimes though.

screen-shot-2016-07-18-at-123208-am.png (193.1 kB)
Comment
Add comment · Show 6
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 CodeElemental · Jul 18, 2016 at 11:16 AM 0
Share

try preserving the z position before reparenting.

 float originalZ = treasure.transform.position.z;
 treasure.transform.SetParent(gameObject.transform, false);
 treasure.transform.position = new Vector2( 
 gameObject.transform.position.x, 
 gameObject.transform.position.y,
 originalZ); 




avatar image theBuilder10000 CodeElemental · Jul 24, 2016 at 05:08 AM 0
Share

Didn't work. I can't tell you why. It happens at complete random, so if I change a few things around it shouldn't matter too much.

avatar image Le-Pampelmuse CodeElemental · Jul 24, 2016 at 06:14 AM 0
Share

Storing the world position and reassigning it right after makes no sense, it will stay the same when setting a new parent.

Why use SetParent and tell it not to keep the world position, and then tell it to keep the world position as before in a new line?

That seem like a complete redundant action to me.

avatar image Le-Pampelmuse · Jul 24, 2016 at 06:02 AM 0
Share
  1. Format your code properly please. Use the 101010 button on it. Edit your question to do that. It's annoying and mostly impossible to read code that is pasted without any formatting. ;)

"How to change an object's parent without changing it's z position" Just change the parent. Use Transform.parent = whateverthenewparentis.

There is not going to be any change of position in the target object if you only change it's parent. The only thing that changes is the localposition because it get's a new parent that this vector refers to.

PS: $$anonymous$$ake sure your read the FAQ and watch this video, if you haven't already.

avatar image theBuilder10000 Le-Pampelmuse · Jul 26, 2016 at 03:13 AM 0
Share

I can't do that. Its not the treasure that the player is colliding with. The treasure is somewhere off screen and teleports on top of the player whenever they touch the statue in the center. I have to change the x and y position.

avatar image Rajeesh_AR · Jul 26, 2016 at 05:26 AM 2
Share

I don't know whether you have a parent currently. If yes, unparent the current one first

treasure.transform.parent = null;

Then assign a parent to treasure.

Also give sorting order for layers (if u r using 2d sprite), so that the one which should come infront can be given priority.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to find position of muzzle. 0 Answers

Rotating parent and positioning child between parent and camera. 1 Answer

Move parent object so that selected child object aligns with sibling 1 Answer

Error in localPosition 1 Answer

How Do I Give Two Objects the Same Transform? 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