Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jan 24, 2020 at 03:03 AM by gabe2o2 for the following reason:

The question is answered, right answer was accepted. Just double-check your understanding of ScreenToWorldSpace, and check your camera settings

avatar image
2
Question by gabe2o2 · Jan 24, 2020 at 02:15 AM · scripting problemerrorbug-perhapstouch controls

Camera.main.ScreenToWorldPoint not working as expected

Hi there. So at the moment, I am trying to get some character movement going based on where a player is touching the screen. Here is the code I have so far

     void Update()
     {
         MoveReady();
     }
 
     public void MoveReady()
     {
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             TouchRegistered(touch);
         }
     }
 
     private void TouchRegistered(Touch touch)
     {
         if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
         {
             //Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, transform.position.y, transform.position.z));
             print(touch.position);
             print(transform.position.x);
             print(touchPosition);
 
             var direction = touch.position.x - transform.position.x;
             print(direction);
         }
     }


I know there isn't any code in there to actually make the player move, but that's because I am not there yet. The problem I am running into is when I run Camera.main.ScreenToWorldPoint(touch.position), I get (0, 0, -10). Now for reference, (0,0,-10) is the position of the camera. I am not sure what I am doing wrong here as this code had worked before, but maybe I'm not seeing something?

Any and all help is greatly appreciated. Thank you for the time!

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

  • Sort: 
avatar image
7
Best Answer

Answer by Bunny83 · Jan 24, 2020 at 02:41 AM

You have a totally wrong idea of what ScreenToWorldPoint does. You have to provide a screen space coordinate in x and y and in z you have to provide the desired distance from the camera in world units. Your usage of transform.position as input for the method makes no sense. This is already a world space coordinate. If your camera is a perspective camera that means at a zero distance (passed in z value of 0) you always get back the camera position since with a perspective camera all perspective lines meet at the camera origin.


We don't know how you want to use the world space coordinate. Though it seems you just want to move your player left and right. For an orthographic camera the distance from the camera doesn't matter. However for a perspective camera you need to use the proper distance to get the right projected position. To calculate the distance of your player from the camera you can use the dot product between the relative vector from your camera to your player and the camera's forward vector

 Vector3 touchPos = touch.position;
 Transform camTrans = Camera.main.transform;
 float dist = Vector3.Dot(player.transform.position - camTrans.position, camTrans.forward);
 touchPos.z = dist;
 Vector3 pos = Camera.main.ScreenToWorldPoint(touchPos);
 
 // do whatever you want with the projected position. For example just set the players x position based on the projected position:
 
 Vector3 tmp = player.transform.position;
 tmp.x = pos.x;
 player.transform.position = tmp;

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 gabe2o2 · Jan 24, 2020 at 02:58 AM 0
Share

Bless up! Thank you! You're definitely correct in saying my understanding of ScreenToWorldPoint was all goofy, but what especially tripped me up was the camera and its perspective setup.

Thank you for your time and for the swift, correct response

avatar image franaboy02official · Jul 21, 2021 at 12:37 PM 0
Share

Hi, I used you method (Bunny83) and I cant get the y axis to work. After tmp.x = pos.x, I wrote tmp.z = -pos.y (I have a view from above) but it aint working. Could you help me?

Follow this Question

Answers Answers and Comments

233 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 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 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 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 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 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 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 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 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 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 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

C# scripts don't open because of the "Debug Error, abort() has been called". Can somebody please help me? 2 Answers

Trying to Invoke method: EndGame.gm.End couldn't be called. 1 Answer

All scripts in unity have stopped working 1 Answer

Grabbing objects for touch controls? how 2 Answers

The type or namespace name x could not be found... 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