- Home /
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;
}
}
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.
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.
Your answer

Follow this Question
Related Questions
Multitouch with two Different Scripts 0 Answers
Box Drag and throw away script for mobile? 0 Answers
Two or more touchs at same time? 0 Answers
How to make function respond to only the initial touch position. 1 Answer
Soft turn with joystick 0 Answers