Hello Guest

Author Topic: Get Mouse or Touch Position Relative to Camera Scale  (Read 4083 times)

dustinbahr

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 76
    • View Profile
Get Mouse or Touch Position Relative to Camera Scale
« on: September 25, 2012, 07:07:17 pm »
Is there any easy way to get the input position relative to the camera?

For instance I have an iPad project that will need to work on retina and non-retina.

I have my tk2dcamera setup as follows:

native resolution: 1536 x 2048
override: 768 x 1024, auto-scale: none, scale: 0.5, fit-mode: center

On non-retina displays when I tap on the edge of the screen, Input.mousePosition reports a number near 768, but the graphics I have in the scene have a width of 1536.

How can I get the position of my input relative to my graphics instead of relative to actual device resolution?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get Mouse or Touch Position Relative to Camera Scale
« Reply #1 on: September 25, 2012, 11:33:35 pm »
In this case, you can use Camera.ScreenToWorldPoint to convert the point into local space. You may need to round the numbers depending on whether the half texel offset is on (d3d on windows). The projection is orthographic, so a z value of znear will do like in the example here:
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html