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 guto-thomas · Jul 09, 2012 at 04:34 PM · camerascreentoworldpoint

ScreenToWorldPoint giving strange results

Hey everyone! I'm working on an element allignment system for my own GUISystem. Something simple, but good for now. Everything works fine except for the ScreenToWorldPoint function, which don't give the results that I was expecting. I don't think there's something wrong with my code. Here it is:

 public static Vector3 allignElement (Transform element, Allignment allignment) {
  
  Camera mainCamera = Camera.mainCamera;
  float distance = mainCamera.nearClipPlane;
  Vector3 GUIZoneElementScale = element.localScale;
  
  if(allignment == Allignment.BottomCenter) {
  
  Vector3 inScreenPos = new Vector3(Screen.width/2, 0, distance);
  return mainCamera.ScreenToWorldPoint( inScreenPos );
  
  } else if (allignment == Allignment.BottomLeft) {
  
  Vector3 inScreenPos = new Vector3(0, 0, distance);
  return mainCamera.ScreenToWorldPoint( inScreenPos );
  
  } else if (allignment == Allignment.BottomRight) {
  
  Vector3 inScreenPos = new Vector3(Screen.width, 0, distance);
  return mainCamera.ScreenToWorldPoint( inScreenPos );
  
  } else if (allignment == Allignment.MiddleCenter) {
  
  Vector3 inScreenPos = new Vector3(Screen.width/2, Screen.height/2, distance);
  return mainCamera.ScreenToWorldPoint( inScreenPos );
  
  } else if (allignment == Allignment.MiddleLeft) {
  
  Vector3 inScreenPos = new Vector3(0, Screen.height/2, distance);
  return mainCamera.ScreenToWorldPoint( inScreenPos );
  
  } else if (allignment == Allignment.MiddleRight) {
  
  Vector3 inScreenPos = new Vector3(Screen.width, Screen.height/2, distance);
  return mainCamera.ScreenToWorldPoint( inScreenPos );
  
  } else if (allignment == Allignment.None) {
  
  return element.transform.position;
  
  } else if(allignment == Allignment.UpperCenter) {
  
  Vector3 inScreenPos = new Vector3(Screen.width/2, Screen.height, distance);
  return mainCamera.ScreenToWorldPoint( inScreenPos );
  
  } else if(allignment == Allignment.UpperLeft) {
  
  Vector3 inScreenPos = new Vector3(0, Screen.height, distance);
  return mainCamera.ScreenToWorldPoint( inScreenPos );
  
  } else if(allignment == Allignment.UpperRight) {
  
  Vector3 inScreenPos = new Vector3(Screen.width, Screen.height, distance);
  return mainCamera.ScreenToWorldPoint( inScreenPos );
  
  }
  
  return Vector3.zero;
  
  }

The problem is when I want an object to be in one of the corners. For instance, UpperLeft gives me this:

alt text

Any idea of why this can be happening?

Thanks from now!

test.jpg (228.2 kB)
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 animusrecro · Jul 09, 2012 at 05:28 PM 1
Share

I think of you are not using an ortho camera u have to use a ray from the camera to get the proper result

1 Reply

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

Answer by aldonaletto · Jul 09, 2012 at 05:28 PM

The nearPlane is too close to your camera - half object will be behind it, and not visible, and the other half will be after it, but still not visible because the back faces are culled out.


You should specify a bigger distance - 10, 20 etc. - to be able to see the objects. But there's a problem: the distance is measured from the camera position, thus the objects will actually be positioned in a spherical surface (radius = distance). If you really need then distributed over a flat surface perpendicular to the camera, you must create a logical plane and use plane.Raycast to find the distance from the ray origin, and ray.GetPoint to find the actual 3D hit point - like this:

... Vector3 GUIZoneElementScale = element.localScale; Vector3 fwd = mainCamera.transform.forward; Vector3 pos = mainCamera.transform.position + fwd * distance; // create a plane at distance, and facing the camera: Plane plane = new Plane(-fwd, pos); Ray ray; // ray to intersect the plane float dist = 0; // will contain the distance along the ray

if(allignment == Allignment.BottomCenter) { ray = mainCamera.ScreenPointToRay(new Vector3(Screen.width/2, 0, 0)); plane.Raycast(ray, out dist); // find the distance of the hit point return ray.GetPoint(dist); // return this point } else if (allignment == Allignment.BottomLeft) { ... Better yet, you could use ViewportPointToRay instead of ScreenPointToRay - the screen coordinates are expressed in the range 0..1, making it easier to specify the positions like left, middle and right (0, 0.5 and 1).

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 guto-thomas · Jul 09, 2012 at 06:09 PM 0
Share

I though about this possibility but I tried to not use raycast... Seems like there's no other way. I'm aware about the ViewportPointToRay, I've already used it! Thanks for your help! Obrigado pela ajuda! :P

avatar image aldonaletto · Jul 09, 2012 at 06:55 PM 0
Share

Brasileiro ou portugues?

avatar image guto-thomas · Jul 09, 2012 at 07:06 PM 0
Share

Brasileiro. :)

avatar image guto-thomas · Jul 09, 2012 at 07:19 PM 0
Share

So... seems like the results were the same as the last ones... Even with raycast, the plane doesn't project himself in the borders. When I select an centered option, it works properly, but when I select an option that places the object in the borders, it doesn't work as it should.

avatar image aldonaletto · Jul 09, 2012 at 11:21 PM 0
Share

Oops! I swapped the Plane arguments - they should be -fwd, pos
Edited the answer to fix this error, and in my tests it worked fine. Remember to set a reasonable value for distance, or the object may even not be visible at all.

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

What makes Camera.ScreenToWorldPoint return different results with same input? 0 Answers

Camera.main.ScreenToWorldPoint works within Unity editor, but not in builds 0 Answers

How to prevent continuous spinning while aiming at the screen's center? 0 Answers

An object who stay in the camera area 1 Answer

Screen to world point - parented camera 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