ScreenToWorldPoint produces two identical output for different input vecotrs
In the update function of my script attached to the main camera, inherited from MonoBehaviour I have a piece of code where there seems to be two different vector inputs to the ScreenToWorldPoint method of the camera component, but they produce identical outputs. I have attached a screenshot and its very self explanatory in the watch list when I debugged my code.
My purpose is to make my camera follow my finger drag smoothly in my 2D game (doesnt seem like too much to ask) Any kind of help is appreciated.
Answer by Glurth · Aug 03, 2016 at 02:37 PM
Note that the Z coordinate in the parameter of Camera.ScreenToWorldPoint is used to specify a distance from the camera. This distance (if positive) is "into" the screen.
If you pass a zero in there for that: because of the way the projection math works, you will always get the camera's position. Instead put a value in there that is the distance from the camera you want. The "screen" itself, is at a distance of the camera's nearClipPlane. https://docs.unity3d.com/ScriptReference/Camera-nearClipPlane.html
e.g. add in
currPos.z = mainCamera.nearClipPlane;
and
lastPos.z = mainCamera.nearClipPlane;
Thanks for the reply, it seemed to have solved the identical output issue, persay. But yet I couldnt achieve what I hoped I would, which was to make camera follow my drag.
Hmm, my guess it this has to do with the fact that your move the camera. So, your call to ScreenToWorldPoint(LastPoint), will return a different value that it did the previous cycle when you called, ScrrenToWorldPoint(currPos), even though you are passing the same screen position. Perhaps if you store the LastPoint as a WORLD position, rather than as screen position you will have better results?
Your answer
Follow this Question
Related Questions
please help i am geting error cs1061 1 Answer
How would do I play a walking animation when i move my joystick? 0 Answers
Detect touch gesture in a more than one camera screen rendering 0 Answers
Sharing ints,Camera position(MOba),warnings,Could loop a fuction in start??? 0 Answers
How to make a dynamic main menu? 0 Answers