Author Topic: Drag & Drop Item positioning problem  (Read 6757 times)

Jason

  • Guest
Drag & Drop Item positioning problem
« 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Drag & Drop Item positioning problem
« Reply #1 on: July 31, 2012, 10:18:38 AM »
Did you scale something leading up to your draggable panel? By say... hmm 30%? :P

Jason

  • Guest
Re: Drag & Drop Item positioning problem
« Reply #2 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...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Drag & Drop Item positioning problem
« Reply #3 on: July 31, 2012, 10:22:11 PM »
Did you set the drag scale to be less by 30% then? Say, 0.7?

Jason

  • Guest
Re: Drag & Drop Item positioning problem
« Reply #4 on: August 01, 2012, 03:17:37 PM »
you mean the Scale setting for my UIDraggable Panel? It's (0, 1, 0).

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Drag & Drop Item positioning problem
« Reply #5 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:

  1. pixelsPerGridSquare = 1.0f / (_mainCam.pixelHeight / (2f * _mainCam.orthographicSize)); // do this as an inverse, so just one division
  2.  

Then, in my mouse drag handler I have this:

  1. mouseDelta *= pixelsPerGridSquare;
  2.  

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.