Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 mwalczuk · Jun 19, 2018 at 06:32 AM · mobiletouchcodepagemovment

Mobile(Android): ScreenToWorldPoint works with Orthographic Camera, fails with Perspective Camera

Goal: Game where player is falling down but is moving on the x axis by following the gamer's finger (follows only press one)
Problem: When in Perspective mode there is simply no effect. I have tries, changing the z value in touchpos but it did not help. Also the movement functions only happen when there is a touch, and only touch 1 is followed. There is no errors.
Solutions to similar problems on this and other forums did not help and the manual provides almost no information about the function in question.
Here is my code: public class PlayerMovment : MonoBehaviour {

 Vector3 camPos;
 Vector3 thisPos;

 Vector3 touchpos;

 Quaternion rot;
 // Use this for initialization
 void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     //Camera Control
     camPos = Camera.main.transform.position;
     thisPos = this.gameObject.transform.position;

     Camera.main.transform.position = new Vector3(camPos.x, thisPos.y-1, camPos.z);
     if (Input.touchCount > 0)
     {
         //Control Position of Player With Touch
         //retrieves touch 0, and sets it's position information to touchpos
         Touch myTouch = Input.GetTouch(0);
         touchpos = myTouch.position;

         //Projects the touch position into a point and than ignores z and y (world touch position saved to pos)
         var pos = Camera.main.ScreenToWorldPoint(touchpos);
         pos.z = transform.position.z;
         pos.y = transform.position.y;

         //Checks if touch is out of bounds, pushes inbounds if so
         if (pos.x > 3)
         {
             pos.x = 3;
         }
         else if (pos.x < -3)
         {
             pos.x = -3;
         }

         //sets the position of the cube to the mapped point
         transform.position = pos;
     }
     //prevents rotation
     rot.x = 0;
     rot.y = 0;
     rot.x = 0;

     transform.rotation = rot;
 }

}

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
0

Answer by Bunny83 · Jun 24, 2018 at 09:41 PM

ScreenToWorldPoint takes a Vector3 as input, not a Vector2. A Vector2 can be implicitly converted into a Vector3, but the "z" coordinate will be set to "0". The z coordinate specifies the distance from the camera origin in worldspace. Since an orthographic camera has a parallel projection an input position at a distance of 0 works as it should. However on a perspective camera all viewport lines meet at the camera origin. So at a distance of 0 all points on the screen will map to a single point which is the camera origin.


As solution you can simply pass a positive distance as z component

 Touch myTouch = Input.GetTouch(0);
 touchpos = myTouch.position;
 touchpos.z = 5;
 var pos = Camera.main.ScreenToWorldPoint(touchpos);

Though it may depend on what you want to achieve. While the z-distance doesn't really matter much for the projected position, when using a perspective camera different distances will result in different positions on x and y due to perspective projection.

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 unityBerserker · Jun 25, 2018 at 09:57 AM 0
Share

I have similar problem. I found solution here https://forum.unity.com/threads/camera-to-object-distance.32643/ I created extention method.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public static class CameraExtensionsUB
 {
     //https://forum.unity.com/threads/camera-to-object-distance.32643/
     /// <summary>
     /// Gets the distance to screen plane in which is gameObject.
     /// </summary>
 
     public static float GetDistanceToPlaneInWhichIsObject (this Camera camera, Transform objPosition)
     {
         Transform cameraTransform = camera.transform;
         Vector3 heading = objPosition.position - cameraTransform.position;
         return Vector3.Dot (heading, cameraTransform.forward);
     }
 
 }

ScreenToWorldPoint as z position take NOT distance in world units from the camera but distance to plane in which is object.

So we must get distance along local z axis of camera to plane in which is object. alt text

odledłosc-od-kamery-1.png (18.6 kB)

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

147 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

Related Questions

Is it possible to have the Unity TouchScreenKeyboard permanently open? 0 Answers

Pressing two buttons simultaneously with the same touch 2 Answers

Deactivate Swipe Controls on Pause? 0 Answers

Touch.ScreenPosition doesnt work ? 0 Answers

Inertia Scrolling Touch 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