Browse Source

Merge pull request #448 from FungusGames/drag-anchor-point

Draggable sprite anchors at exact point user clicked
master
Chris Gregan 9 years ago
parent
commit
2499d97b20
  1. 11
      Assets/Fungus/Sprite/Scripts/Draggable2D.cs

11
Assets/Fungus/Sprite/Scripts/Draggable2D.cs

@ -33,9 +33,16 @@ namespace Fungus
protected Vector3 startingPosition;
protected bool updatePosition = false;
protected Vector3 newPosition;
protected Vector3 delta = Vector3.zero;
protected virtual void OnMouseDown()
{
// Offset the object so that the drag is anchored to the exact point where the user clicked it
float x = Input.mousePosition.x;
float y = Input.mousePosition.y;
delta = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - transform.position;
delta.z = 0f;
startingPosition = transform.position;
foreach (DragStarted handler in GetHandlers<DragStarted>())
@ -55,7 +62,7 @@ namespace Fungus
float y = Input.mousePosition.y;
float z = transform.position.z;
newPosition = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f));
newPosition = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - delta;
newPosition.z = z;
updatePosition = true;
}

Loading…
Cancel
Save