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 AW0610AUT · Jul 28, 2014 at 07:35 PM · guiinstantiatescreentoworldpointviewporttoworldpointshowcase

Camera.main.ViewportToWorldPoint not working like expected.

Hello everybody,

I am writing a script that Showcases Objects that you click. For example a Sodacan on the floor will be displayed at the screen above a gui that shows some stats and a bit of text + a 3D Model of the clicked Object. Here is a bit of the code i have (without Gui stuff):

 var open:boolean = true;
 var close:boolean = false;
 
 var Inst_Obj:boolean = false;
 
 var ShowcasePos: Vector3;
 var ShowcaseObj: Transform;
 
 
 var GUIStyle1: GUIStyle;
 var GUIStyle2: GUIStyle;
 var GUIStyle3: GUIStyle;
 
 var Player: GameObject;
 
 
 var rotation: float;
 
 
 
 function Update () {
     ShowcasePos = Camera.main.ViewportToWorldPoint (Vector3 (Screen.width/2,Screen.height/2,0));
     Player = gameObject.FindWithTag ("Player");
     
     var distance = Vector3.Distance(transform.position, Player.transform.position);
     
     if (distance < 3)
         close = true;
     else
         close = false;
     
     if (close == false)
         open = false;
 
 
     if (open && Inst_Obj == false){
         
         ShowcaseObj = Instantiate (transform, ShowcasePos, Quaternion.identity);
         ShowcaseObj.rigidbody.useGravity = false;
         ShowcaseObj.gameObject.layer = 8;
         Inst_Obj = true;
     }
     
     ShowcaseObj.position = ShowcasePos;
 }
 

So it works but i have problems with positioning it (the ShowcaseObj) on the screen. So how can i use the ViewportToWorldPoint function to display it in the right Position? When I leave the Y Position on the Vector3 zero it spawns the Object right at the camera. But when I use 1,2,3... it spawns at a completely random position very far away from the camera. What am i doing wrong?

Thanks for the help in advance.

-Alexander

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
3
Best Answer

Answer by robertbu · Jul 30, 2014 at 05:39 PM

You have a couple of problems here. First viewport coordinates go from (0,0) in the lower left corner to (1,1) in the upper right corner. You are passing the ViewportToWorldPoint() Screen coordinates that are out of bounds of the viewport. For the center of the screen, use 0.5 for x and y, or switch to using ScreenToWorldPoint().

The second issue is see is the 0.0 for the 'z' value. The 'z' value for both ScreenToWorldPoint() and ViewportToWorldPoint() is the distance in front of the camera for the point calculation. So you want to set 'z' to some positive value before making the call.

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 AW0610AUT · Jul 30, 2014 at 07:37 PM 0
Share

Thank you. I switched to ScreenToWorldPoint and it works perfectly. I know I´m supposed to make a new question but really quick: How can I render the Object infront of the GUI? I´m using a different camera with a higher depth but it still is below the GUI.

avatar image robertbu · Jul 30, 2014 at 08:16 PM 1
Share

I don't know the answer, but a quick search gives me an answer by someone I trust:

http://answers.unity3d.com/questions/30230/rendering-a-camera-in-front-of-a-gui-element.html

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

Problems with ScreenToWorldPoint in 2D game 1 Answer

Viewport to World Coordinates 2 Answers

mouse Input Instantiate 0 Answers

Instantiate Game object at clicked position? 3 Answers

How to activate a button? 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