Hello Guest

Author Topic: Issues with Gameobject's position and Input.mouseposition  (Read 5870 times)

BraveSirRobin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Issues with Gameobject's position and Input.mouseposition
« on: January 06, 2013, 07:38:20 pm »
Hi,

I am using a tk2dCamera with a native resolution of 768x1024 (iPad Wide), and I've also created a 480x320 override (iPhone wide).

I have several draggable tk2dsprites, and what I want to do is:

- When I click and hold on a sprite I want to drag them around the screen
     - This is working fine

- When my Input.mousePosition lands inside some given predefined boxes I want to lock the tk2dsprite within the box
     - Input.mousePosition information is also fine on different screen resolutions (i.e.: Top right on the iPad reads x: 768.0 and y: 1024.0, on the iPhone it reads x: 480.0 y: 320.0).
     - I've defined the boxes as empty gameObjects with box colliders, only to have a reference on the size of the lock boxes compared with my background sprite, I didn't intended to use them to check collision

And this is where my problems begin...

If I check the position of the box colliders when running on iPad wide everything's fine. As reference, one of the boxes' transform.position read (106.0, 655.0, 500.0f). Placing it on the top left corner of my screen. And the game works as expected, drag and drop the sprites on the boxes work like a charm.

However, when I switch the platform to iPhone, the sprites rescale correctly, but the same box reads the same transform.position of (106.0, 655.0, 500.0), effectively putting it OUTSIDE of the screen (since the iPhones' height is 480).

I hope the description is clear enough... What am I doing here?

Thanks in advance

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Issues with Gameobject's position and Input.mouseposition
« Reply #1 on: January 07, 2013, 02:02:31 am »
The issue is that Input.mousePosition isn't getting scaled and offset for the camera when overrides are applied.

The easiest way to handle this is to use Camera.ScreenToWorldPoint to convert the screen space point from Input.mousePosition to world space. You can then simply use the value directly. You will get the values rescaled to the native resolution.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Issues with Gameobject's position and Input.mouseposition
« Reply #2 on: January 07, 2013, 02:12:24 am »
Also, I should add that tk2dCamera overrides work by changing the camera so it views the same data but at a different scale & offset. That means you don't have to reposition everything, but the downside is the coordinates are the same regardless of actual resolution, so you will need to transform your physical mouse/touch position before using it.

Another alternative is to use colliders - that way you can avoid using hard coded positions altogether. Refer to the button sample & code for more info on this.

BraveSirRobin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Issues with Gameobject's position and Input.mouseposition
« Reply #3 on: January 07, 2013, 02:51:06 am »
The issue is that Input.mousePosition isn't getting scaled and offset for the camera when overrides are applied.

The easiest way to handle this is to use Camera.ScreenToWorldPoint to convert the screen space point from Input.mousePosition to world space. You can then simply use the value directly. You will get the values rescaled to the native resolution.

Actually Input.mousePosition was being scaled (As I've mentioned originally Top right on the iPad reads x: 768.0 and y: 1024.0, on the iPhone it reads x: 480.0 y: 320.0))

Also, are you sure it's ScreenToWorld?

Because I've used ScreenToWorld at first, and I got weird results (like Y coordinate in the thousands in the iPhone wide).

When I've switched to WorldToScreen and it started working (at least on iPad wide and iPhone wide, haven't tested it in the other platforms, but I don't see why it wouldn't...).

I'll check the button sample anyway.

One other question: My box colliders (which I am using to lock the position) are scaled to X = 60 and Y = 60 in the iPad version, but they still keep this scale in the iPhone version, is there any way I can automatically rescale my box colliders as well, even though it does not have (at least not for now) any tk2d component?

Thanks again!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Issues with Gameobject's position and Input.mouseposition
« Reply #4 on: January 07, 2013, 03:22:16 am »
The box collider shouldn't have to scale when the resolution changes, as the physical size of the sprite remains the same.

Using ScreenToWorld gives you correct results, just tested it again just in case I was mixing it up with something else.
What specifically does your 480x320 override do? If you use a wildcard override (the default one the button creates), then everything should definitely scale correctly.