Support => NGUI 3 Support => Topic started by: Jason on July 31, 2012, 08:55:11 AM
Title: Drag & Drop Item positioning problem
Post by: Jason on July 31, 2012, 08:55:11 AM
Sorry to bother your guys again, I am really spoiled by your fast responses :)
I modified your Drag N Drop scripts to suite my own needs and I created a strange problem. My items are lagging behind my cursor. To compensate for it, I applied a factor of 1.35f (not 1.3333f, which was my initial estimate):
mTrans.localPosition += (Vector3)delta * 1.35f;
I didn't find any weird scalings in my game objects. Any idea what could be the cause of it?
Title: Re: Drag & Drop Item positioning problem
Post by: ArenMook on July 31, 2012, 10:18:38 AM
Did you scale something leading up to your draggable panel? By say... hmm 30%? :P
Title: Re: Drag & Drop Item positioning problem
Post by: Jason on July 31, 2012, 01:47:20 PM
Nope :-[ In fact, mTrans.lossyScale give the same scaling factors for UIRoot. I tried Input.mousePosition, same thing. It looks like a scaling problem, only I couldn't find any scaling. Maybe I just discovered that the cyberspace is expanding as well...
Title: Re: Drag & Drop Item positioning problem
Post by: ArenMook on July 31, 2012, 10:22:11 PM
Did you set the drag scale to be less by 30% then? Say, 0.7?
Title: Re: Drag & Drop Item positioning problem
Post by: Jason on August 01, 2012, 03:17:37 PM
you mean the Scale setting for my UIDraggable Panel? It's (0, 1, 0).
Title: Re: Drag & Drop Item positioning problem
Post by: JRoch on August 01, 2012, 03:47:12 PM
I'm pretty sure that NGUI keeps everything scaled so you can directly use mouseDelta without any scaling under normal circumstances. However, if you're dealing with a situation where the ratio of the camera's orthographicsize isn't 1:1 with the camera's pixelheight in terms of world units per pixel square, you'll find that mouseDelta is then 1 unit <> 1 pixel. Again, normally this shouldn't crop up, but you said you did some customization, so you may have introduced a situation where the ratio of units to pixels isn't 1:1.
For what I'm working on I needed to be able to drag around a map area where grabbing a specific visible object and dragging it would result in the result of the object's coordinates *exactly* lining up with the cursor as it is dragged. For this you have to calculate the ratio of world units to pixels.
On app startup, and whenever the screen size changes, I call something that includes this:
pixelsPerGridSquare = 1.0f /(_mainCam.pixelHeight/(2f * _mainCam.orthographicSize));// do this as an inverse, so just one division
Then, in my mouse drag handler I have this:
mouseDelta *= pixelsPerGridSquare;
The result is that the mouse drag in pixel (screen) space is properly scaled into the scene's world space and the calculated position deltas for the gameobject (or camera) transform lines up visually with what you see on the screen in terms of mouse motion.