From 3e3359bc7fd901f54cf0bac29e1cb840c2271300 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Fri, 22 May 2015 11:23:27 +0100 Subject: [PATCH] Fix iTween overriding position when dragging an object #107 --- Assets/Fungus/Sprite/Scripts/Draggable2D.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Assets/Fungus/Sprite/Scripts/Draggable2D.cs b/Assets/Fungus/Sprite/Scripts/Draggable2D.cs index c3b58f2d..c67c03d3 100644 --- a/Assets/Fungus/Sprite/Scripts/Draggable2D.cs +++ b/Assets/Fungus/Sprite/Scripts/Draggable2D.cs @@ -24,6 +24,9 @@ namespace Fungus protected Vector3 startingPosition; + protected bool updatePosition = false; + protected Vector3 newPosition; + protected virtual void OnMouseDown() { startingPosition = transform.position; @@ -45,12 +48,22 @@ namespace Fungus float y = Input.mousePosition.y; float z = transform.position.z; - Vector3 newPosition = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)); + newPosition = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)); newPosition.z = z; + updatePosition = true; + } - transform.position = newPosition; + protected virtual void LateUpdate() + { + // iTween will sometimes override the object position even if it should only be affecting the scale, rotation, etc. + // To make sure this doesn't happen, we force the position change to happen in LateUpdate. + if (updatePosition) + { + transform.position = newPosition; + updatePosition = false; + } } - + protected virtual void OnMouseUp() { if (!dragEnabled)