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
3
Question by prak · Jun 25, 2013 at 04:33 PM · worldspacescreenspace

How to convert Screen Units to World Units

I am trying to figure out how to do a unit conversion between screen and world.

For example. A player drags their finger 20 screen units, how many units in world space would that be?

I'm not looking for Camera.main.ScreenToWorldPoint. but how to find the unit conversion.

EDIT:

I wanted to add why I need the units... The Input.touches provides a delta position, but it's in screen space. I need to use this but to move something in world space. The Camera is moving, so the Camera.main.ScreenToWorldPoint is not going to work.

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

4 Replies

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

Answer by Coderdood · Jun 25, 2013 at 04:54 PM

Your probably going to have to use ScreenToWorldPoint. If you need a range you could use:

 StartPoint = ScreenToWorldPoint(FingerStart);
 EndPoint = ScreenToWorldPoint(FingerEnd);
 Range = EndPoint - StartPoint;

Or something like that. Is there a particular reason ScreenToWorldPoint isn't what your looking for?

You could in theory do the math and figure it out - but unless you have a specific reason to not use ScreenToWorldPoint it would be a waste of effort. The world point will depend on a number of factors including camera angle, position and projection that unless you have a fixed camera would change over time.

Basically:

At frame 0: 20 Screen Units may equal 100 World Space units

At frame 1: 20 Screen Units may equal 50 World Space units

At frame 2: 20 Screen Units may equal 1000196 Worlds Space units

So tldr: Just use ScreenToWorldPoint.

Comment
Add comment · Show 4 · 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 prak · Jun 25, 2013 at 04:57 PM 0
Share

That's exactly what I was looking for, I need to do a start and end to get the right measurement. Thanks

avatar image prak · Jun 25, 2013 at 09:05 PM 2
Share

This is how I solved my problem:

             Vector3 p1 = Camera.main.ScreenToWorldPoint(Vector3.zero);
             Vector3 p2 = Camera.main.ScreenToWorldPoint(Vector3.right);
             float unit = Vector3.Distance(p1, p2);
             Vector3 pt = new Vector3(Camera.main.transform.position.x - touchDelta.x * unit, Camera.main.transform.position.y - touchDelta.y * unit, Camera.main.transform.position.z);
             
avatar image codebeans prak · Feb 26, 2016 at 07:25 PM 3
Share
 using UnityEngine;
 
 namespace Assets.Scripts.Other {
     public static class CameraExtension {
 
         public static float UnitsPerPixel(this Camera cam) {
             var p1 = cam.ScreenToWorldPoint(Vector3.zero);
             var p2 = cam.ScreenToWorldPoint(Vector3.right);
             return Vector3.Distance(p1, p2);
         }
 
         public static float PixelsPerUnit(this Camera cam) {
             return 1/UnitsPerPixel(cam);
         }
 
     }
 }


Nice Idea, I made them extension methods

avatar image slek120 · Mar 16, 2014 at 10:59 AM 1
Share

When you use Camera.main, you are actually doing a tag search for the first camera with the tag "$$anonymous$$ainCamera". If you are doing this every frame, you might want to first cache a reference to the camera in the Start function.

 Camera mainCamera;
 void Start()
 {
     mainCamera = Camera.main;
 }
 void Update()
 {
     if (Input.touchCount > 0){
         Vector2 touchPos = Input.GetTouch(0).position;
         Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(touchPos.x, touchPos.y, 0));
     }
 }

Also wanted to mention that a moving camera doesn't mean the world points are wrong. The world points are relative to the camera's position.

avatar image
2

Answer by slek120 · Mar 16, 2014 at 12:44 PM

This can give you a conversion, although it's not the same as camera.ScreenToWorldPoint.

 Vector2 test = Random.insideUnitCircle * Screen.height;
 float height;
 if (camera.orthographic)
     // Size is half of the height of the screen
     height = camera.orthographicSize;
 else
     // Since units depends on how far the camera is to the plane that you are displaying on, trig becomes necessary
     // As you can imagine, this will be really annoying if your camera or your plane is rotating
     // I assume here that the plane is at origin and normal points to camera
     height = Mathf.Tan (camera.fieldOfView * 0.5f) * Vector3.Magnitude (camera.transform.position);
 // Both ortho and perspective cameras calculate width using height
 float width = height * camera.aspect;
 // First convert to a box with coordinates (-0.5,-0.5) to (0.5,0.5)
 // Then scale to world units
 Vector3 testPos = new Vector3 (width * (test.x / Screen.width - 0.5f), height * (test.y / Screen.width - 0.5f), 0);

And that is why I use camera.ScreenToWorldPoint

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 slek120 · Mar 16, 2014 at 11:50 AM 0
Share

Also, a world unit is supposed to be a meter, which is 100px when you import textures. Since iPhone5 resolution is 640x1136, an orthographic camera of size 5.68 (=1136/2/100) will give you the exact size for a texture of size 640x1136

avatar image
0

Answer by TheDarkVoid · Jun 25, 2013 at 04:54 PM

This would be fairly simple to accomplish. First you need to look at Camera.ScreenPointToRay(). On the start of the drag you will use Camera.ScreenPointToRay() and save the hit point, and do the same at the end on the drag. You can then use Vector3.Distance() to find the distance between the two points.

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 NymoBasepro · Jul 29, 2017 at 09:57 PM

@youtilities Thanks men, this is the first usefull thing I read about it!

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

20 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

Related Questions

World and Screen space Multiplayer[Touch Screens] 1 Answer

How can I determine what tile I'm clicking on 0 Answers

How to display the gameobjects of a worldspace canvas in front of gameobjects of screen space overlay canvas? 1 Answer

how to get the most right point or left point atc of object as visual on the screen in 3d project? 0 Answers

screenspace to worldspace, not working 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