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 omatase · Sep 02, 2014 at 01:53 AM · vector3coordinatesscreentoworldpointworldtoscreenpoint

Help positioning an object on a 2D plane

I have a prefab on top of which I'm trying to position another game object but I can't figure out the positioning. It's really confusing to me. Here is the code I thought should work (comments show the actual run time values I'm working with).

 Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position); // value (496.0, 558.0, 5)
 Vector3 offset = new Vector3((renderer.bounds.size.x * 100) / 2, 0); // value (575.5, 0, 0)
 Vector3 newPosition = screenPos - offset; // value (-79.5f, 558.0, 5.0) 

When I draw an object at position screenPos I get something in the very center of my prefab so I know that "screenPos" is a position in the center of my prefab.

From there, though things are a bit merky. From my understanding of the Unity coordinate system the value in screenPos should be the ScreenPoint representation of the absolute position of the center of my game object in relation to the entire window. The offset value should be half the horizontal size of the game object in the same unit system as my screenPos and the newPosition variable should be an absolute position on the very far left of my game object in relation to the entire window. However ... something no es bueno because it is just not working. As you can see from the 3rd line above the x value is negative so won't be displayed on screen.

Here is a shot showing where my prefab is in the window for reference. The red box shows the position I'm trying to calculate with my code.

alt text

example.jpg (90.3 kB)
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

1 Reply

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

Answer by omatase · Sep 02, 2014 at 03:35 AM

Well gosh darnit 5 days of trying to understand this and after banging on it myself for that long I finally figured it out. The problem I'm having with this specific code is it doesn't account for scaling. I just assumed that if I did nothing it would keep my image assets sized at their default size. Of course that's not the case, I just hadn't thought it through until now.

Also, honestly this is the first time I've ever used an orthographic camera and so I had to learn the hard way how to use it. Now, knowing how to use it the problem is so simple to solve.

Here's the fixed code:

 Vector3 screenPos = transform.position - new Vector3((renderer.bounds.size.x / 2), 0, 0);
 
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

22 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

Related Questions

Understanding Unity's coordinate system 3 Answers

How can touch input directly control an orthographic camera? 1 Answer

Move Object - Mouse Input Y Axis from Camera 0 Answers

WorldToScreenPoint result of Player Object position with bad Y/Z values 1 Answer

Problem with ScreenToWorldPoint and touches (C#) 2 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