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 Anthonylinx · Nov 14, 2013 at 05:29 AM · camerascreen

Camera.ScreenToWorldPoint Not Giving Right Point

So I have to following lines of code and all I'm trying to do is set tempTiles position to the bottom left hand corner of the screen. However, with these lines of code the position is not 0,0,0 but rather -8, -4, 0. Could someone please tell me why this is happening? Also if it's worth mentioning I'm working in 2D.

  Vector3 screenPoint = new Vector3(0,0,0);
 
 Vector3 worldPos = Camera.main.ScreenToWorldPoint (screenPoint );
 
 Quaternion ZeroRotation = new Quaternion(0,0,0,0);
 
 GameObject tempTile = (GameObject)Instantiate(Resources.Load("Tile"),worldPos,ZeroRotation);
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

3 Replies

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

Answer by Aram-Azhari · Nov 14, 2013 at 07:26 AM

If you want the object to be at the position 0,0,0 , just write:

 Vector3 worldPos = new Vector3(0,0,0)

But if you want the object to be at the bottom left, what you do is correct, but don't expect it to be 0,0,0 in the world. Your comparison is wrong.

In order to check that you have set the right position, add these lines after the gameobject creation:

 Vector3 screenPos = Camera.main.WorldToScreenPoint(tempTile.transform.position);
 Debug.Log(screenPos);

As you can see, it is still 0,0,0 which means you have correctly set the place of the object at the corner of the screen.

Comment
Add comment · Show 7 · 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 Anthonylinx · Nov 14, 2013 at 07:41 PM 0
Share

If all I wanted to do was set an objects position in screen coordinates could you tell me how I would do that?

avatar image Aram-Azhari · Nov 14, 2013 at 07:54 PM 0
Share

Actually what you're doing is correct. I tested your code and if you look at the editor, it places it on the corner. However, since you are using Camera's worldtoscreenpoint, your object is placed just at the camera's z level. What I mean is, when the worldPos is (0,0,0) the object will also be at (something,something, 0).

What you should do is to change the line to:

 Vector3 worldPos = new Vector3(0,0,10) 

or any other number so the object will be in front of the camera.

avatar image Anthonylinx · Nov 14, 2013 at 08:02 PM 0
Share

So, if I understand correctly my object actually is being drawn at the bottom left hand corner but I just can't see it as it's being drawn on the same z of the camera?

avatar image Aram-Azhari · Nov 14, 2013 at 08:03 PM 0
Share

exactly. At least It did when I tested you code.

avatar image Anthonylinx · Nov 14, 2013 at 08:04 PM 0
Share

Alright thank you very much, I'll have to try and change that when I get a chance.

Show more comments
avatar image
0

Answer by DanPHernbrott · Nov 14, 2013 at 07:17 AM

Specifically, the SCREEN position is (-8, -4, 0)? Or the world position? Because the world position could potentially be anything, depending on the camera location.

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
avatar image
0

Answer by Jix · Nov 14, 2013 at 08:17 PM

This is not the right way to do it.

There are 2 ports to deal with the camera, screen bottom left corner is not (0,0), it has different value, a negative one... what you need is ViewPort not screenPoint. you shoud do this instead

 Vector3 screenPoint = new Vector3(0,0,0);
 Camera.main.ViewportToWorldPoint(screenPoint );
Comment
Add comment · Show 3 · 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 Aram-Azhari · Nov 14, 2013 at 08:27 PM 0
Share

Well technically this isn't the wrong way, but when you have only one camera where the view port is exactly the same as your screen, this won't be necessary. But of course, in a case you have a small map of your RTS game where that map is another camera, this makes sense.

avatar image Jix · Nov 14, 2013 at 09:41 PM 0
Share

What are the odds that his bottom left of his screen will be exactly at (0,0,0)?

avatar image Aram-Azhari · Nov 14, 2013 at 10:27 PM 0
Share

It isn't something you'd estimate, it is something you can calculate. It is always 0,0,0. Unless you post a code that runs otherwise. But hey, if viewport does it without any odds, then users should try that.

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

19 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

Related Questions

Lock the screen 1 Answer

Camera Shake 4 Answers

Clicking Trigger 1 Answer

Orthographic cameras and screen resolutions 1 Answer

Camera setup question 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