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 Askiparait · Jan 26, 2020 at 05:27 AM · gameobjectcanvaslocalposition

Position of a gameObject compared to Canvas (or other)

Hello, I want to check the localPosition of a gameObject compared to the Canvas, but this gameObject is a great great great (etc) grandson of another gameObject. How could i do ? Thanks !

Comment
Add comment · Show 1
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 TreyH · Jan 29, 2020 at 03:02 PM 0
Share

What is "compared to" here, btw? Are you expecting to get pixels or normalized positions within the canvas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RahulRaman · Jan 27, 2020 at 06:24 AM

Code

1st copy paste the following code in a new c# script in unity assets folder

2nd attach it to the gameobject whose position you want to know compared to canvas's game position

And at last refrence the canvas in the scene in unity editor inspector.

Following is the code with pictures to show it working:-

 using UnityEngine;
 
 
 public class testable : MonoBehaviour
 {
     [SerializeField] Transform canvas;
     void Start()
     {
         // Get the desired result and store in a variable
         Vector3 localPositionParentingCanvas = transform.position - canvas.position;
 
         // Show the result Vector3 in console logs
         Debug.Log(localPositionParentingCanvas);
     }
 }


screenshot

Explanation

When a gameobject is given a new parent it's local space changes therefore localPosition changes, but position is not changed as it is in the world space. The new localPosition is a Vector3 position in the space which has the new parent as the origin. Therefore in the new local space the parent transform has position Vector3.zero

What I do in the code is to calculate the displacement of origin and then add it to the world position of the gameobject. I do this because each position is displaced by equal magnitude on all axes in the new local space. And the code I have given above is the short hand of the long code to do that.

Following would be the full long form of the code to do this as I explained above.

         // Original origin in world space is (0,0,0)
         Vector3 OldWorldOrigin = Vector3.zero;

         // World space of the new origin is the position of canvas
         Vector3 NewLocalOrigin = canvas.position;

         // Claculate the displacement of the origin
         Vector3 displacement = OldWorldOrigin - NewLocalOrigin;

         // Get the localPosition of our gameobject by adding displacement,
         // since displacement remains same for all gameobjects
         Vector3 localPositionParentingCanvas = transform.position + displacement;

This can be shortened in one line as :

             Vector3 localPositionParentingCanvas = transform.position + Vector3.zero - canvas.position;

Since Vector3.zero is 0 on all axes thus Vector3.zero - newVector3 = (-newVector3), and so the final short form of code comes to be:

                 Vector3 localPositionParentingCanvas = transform.position - canvas.position;

Explaining image


screenshot.png (191.8 kB)
explanation.png (175.2 kB)
Comment
Add comment · Show 2 · 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 Askiparait · Jan 29, 2020 at 02:19 PM 0
Share

Thanks, but I don't see how it could be useful, as i want the Vector2 or Vector3 of the gameObject as a localPosition if it would be the child of the Canvas.

avatar image RahulRaman Askiparait · Jan 31, 2020 at 12:16 PM 0
Share

@Askiparait

I have updated my answer to be more specific about the topic and included a screenshot to show it working.

If you set the canvas gameobject as refrence to the canvas variable in unity inspector you will get the result you want and the result will be a Vector3 as the localPosition if the transform script is attached to is the child object of canvas but in the 3d world.

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

192 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving a canvas relative to a gameobject's position 1 Answer

Canvas shrinks over time 0 Answers

how can I add a 2D label to a 3D GameObject? 2 Answers

Changing alpha value of a canvas from a different game object 1 Answer

Masking GameObject in Canvas 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