Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Tomer-Barkan · Jul 29, 2012 at 04:50 PM · transformpositionworld space

Objects with same position appear in different places

Hi,

I'm having problems comparing location of two different objects. I have a ball and a basket hoop, and I want to calculate where they are located relative to each other, so I try comparing the "transform.position" of both objects, but get incorrect results.

According to documentation, position relates to world space and not local space, so I assumed that is absolute position. Then I saw my ball moving down instead of towards the hoop, and upon inspection found that the hoop's "y" position appears smaller than the ball's, even though it is obviously higher in the graphic display.

Then I tried setting them both to (0,0,0), but they still appear in different locations visually.

So I assume that my understand of "world space" is incorrect, but I couldn't find any other property in the API or in the inspector that would tell me the absolute location of the objects in my scene.

Any help understanding these concepts would be appreciated, Google was not able to help...

Edit: The scene was created by a friend imported from Maya.

Edit2: It appears that the hoop's center/pivot (not sure which) is in a different location than the object's mesh appears in. Why is that?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Kryptos · Jul 30, 2012 at 10:31 AM

Possibly the center of both object is not correctly "centered".

Check that the scene gizmo of the transform is positionned at the center on each object. If not put the wrong-positionned object under another GameObject (parenting) and correct the local position so that the object is centered relative to its parent (i.e. when selecting the parent, the scene gizmo appears at the center of the object).

Comment
Add comment · Show 1 · 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 Tomer-Barkan · Jul 30, 2012 at 02:48 PM 0
Share

Ok, I think this was it. In the GUI I can select "Center" or "Pivot". When I select "Pivot" I can see that the tool is in the same position for both objects even though the object's mesh appears in a different position...

So I guess this has something to do with how unity imported the objects from $$anonymous$$aya?

I'll add a screenshot to my question.

avatar image
0

Answer by Bunny83 · Jul 29, 2012 at 04:54 PM

Generally Unity works with local space coordinates (localPosition). In the inspector you will always see the localPosition and localEulerAngles.

.position will let you access it's world position. This is just a wrapper property. When you set position of an object that is a child of another object, Unity will calculate the correct local position the object needs to appear at the given world position.

Same thing for rotation / eulerAngles.

Comment
Add comment · Show 5 · 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 Tomer-Barkan · Jul 30, 2012 at 05:49 AM 0
Share

Still - from my script I checked the ".position" and it showed the same numbers as the inspector.

I tried calculating the difference between the ".position" of the hoop and the ball, and my calculations ended up with the ball going the opposite direction... (of course I subtracted the ball's position from that of the hoop, not the other way around)

Any ideas why?

avatar image Bunny83 · Jul 30, 2012 at 10:23 AM 0
Share

We have still no idea what you're actually doing. how do they move in different directions? how do you move them? Are the objects rotated? Are you sure both objects don't have a parent object?

$$anonymous$$aybe you can post the code you use for the movement and maybe a screnshot which shows your described behaviour.

avatar image Tomer-Barkan · Jul 30, 2012 at 02:52 PM 0
Share

I'm trying to give throw the ball towards the hoop when the player presses a button.

Here's a code snippet (C#) and I added a screenshot to my post:

void Update () { if (Input.GetButtonDown("Jump")) { Transform hoop = GameObject.Find("Basket_Hoop").transform; float[] hoopPosition = vectorToArray(hoop.position); float[] ballPosition = vectorToArray(transform.position);

float[] velocity = vectorToArray(rigidbody.velocity); for (int i = 0; i < 3; i++) { if (i == 1) { velocity[i] += speed; continue; } velocity[i] = (hoopPosition[i] - ballPosition[i]); }

Debug.Log("altitude:" + transform.position.y); rigidbody.velocity = arrayToVector(velocity); } }

avatar image Bunny83 · Jul 31, 2012 at 11:07 AM 1
Share

Your code is very strange. Why do you convert the vectors into float arrays? It's much easier this way:

 Transform hoop;

 void Start()
 {
     hoop = GameObject.Find("Basket_Hoop").transform; 
 }

 void Update()
 {
     if (Input.GetButtonDown("Jump"))
     {
         Vector3 newVelocity = hoop.position - transform.position;
         newVelocity.y = rigidbody.velocity.y + speed;
         
         rigidbody.velocity = newVelocity;
     }
 }

But your real problam is probably that your hoop objects pivot is somewhere at the base plate, and not at the hoop position. Select your hoop object, make sure the pivotmode is set to pivot and not to center.

then you will see where the pivot of that object is. This position is the world position of that object.

As solution you could add an empty gameobject as child to your hoop object at the desired position and then use this object for your calculations.

avatar image Tomer-Barkan · Jul 31, 2012 at 11:13 AM 0
Share

Thanks, I asked my friend that created the models and he made new ones with correct pivot points...

BTW - I'm completely new to C# and unity so not familiar with all the APIs... thanks for the tip about subtracting vectors.

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

7 People are following this question.

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

Related Questions

Moving object with transform.position ignore other objects even if they collided 1 Answer

Need enemy to follow only active character 0 Answers

Drawing a (debug) line between anchored UI element and mouse? 0 Answers

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

How to move an object to your current positon? 0 Answers


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