1313
1414namespace JCSUnity
1515{
16-
1716 /// <summary>
1817 /// Move a game object in certain distance then set the game object
1918 /// back to original position.
@@ -39,16 +38,22 @@ public class JCS_3DDistanceTileAction
3938 private bool mActive = true ;
4039
4140 [ Tooltip ( "How long this game object could tracvel." ) ]
42- [ SerializeField ] [ Range ( 0 , 30000 ) ]
41+ [ SerializeField ]
42+ [ Range ( 0 , 30000 ) ]
4343 private float mDistance = 0 ;
4444
45+ [ Tooltip ( "Use the local position instead of global position." ) ]
46+ [ SerializeField ]
47+ private bool mUseLocalPosition = false ;
48+
4549 //----------------------
4650 // Protected Variables
4751
4852 //========================================
4953 // setter / getter
5054 //------------------------------
5155 public bool Active { get { return this . mActive ; } set { this . mActive = value ; } }
56+ public bool UseLocalPosition { get { return this . mUseLocalPosition ; } set { this . mUseLocalPosition = value ; } }
5257
5358 //========================================
5459 // Unity's function
@@ -58,7 +63,10 @@ private void Start()
5863 // NOTE(jenchieh): prevent if other component want to change
5964 // this position, we have the record position at function
6065 // 'Start' rather than 'Awake'
61- this . mOriginPos = this . transform . localPosition ;
66+ if ( mUseLocalPosition )
67+ this . mOriginPos = this . transform . localPosition ;
68+ else
69+ this . mOriginPos = this . transform . position ;
6270 }
6371
6472 private void Update ( )
@@ -68,14 +76,21 @@ private void Update()
6876
6977 // Find out the distance between the current moved
7078 // position and the starting position.
71- float distance = Vector3 . Distance ( this . transform . localPosition , mOriginPos ) ;
79+ float distance = 0.0f ;
80+ if ( mUseLocalPosition )
81+ distance = Vector3 . Distance ( this . transform . localPosition , mOriginPos ) ;
82+ else
83+ distance = Vector3 . Distance ( this . transform . position , mOriginPos ) ;
7284
7385 // check if the distance reach?
7486 if ( distance < mDistance )
7587 return ;
7688
7789 // set back to original position.
78- this . transform . localPosition = this . mOriginPos ;
90+ if ( mUseLocalPosition )
91+ this . transform . localPosition = this . mOriginPos ;
92+ else
93+ this . transform . position = this . mOriginPos ;
7994 }
8095
8196 //========================================
0 commit comments