Author Topic: TweenPosition only fires from OLD position!  (Read 3193 times)

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
TweenPosition only fires from OLD position!
« on: March 13, 2013, 10:46:25 PM »
basic concept:
tween a panel BY (300,0,0). (as in, move from its CURRENT position 300 to the right).
later, tween same panel (0,50,0). As in, move from its CURRENT position 50 up).

I wrote this, and it SHOULD work, but instead of moving it from it's current position, it ONLY moves the panel from it's ORIGINAL position. So the first tween slides it right like expected, but the next one SNAPS it back to 0,0,0 and tweens it up.

   
  1. static void Shift (GameObject menu, float duration, Vector3 shift)
  2.         {
  3.                 Vector3 pos = menu.transform.position;//starting position
  4.                 Debug.Log(string.Format("current x:{0},y:{1},z:{2}",pos.x, pos.y, pos.z));
  5.                 Debug.Log(string.Format("Shift x:{0},y:{1},z:{2}",shift.x, shift.y, shift.z));         
  6.                
  7.                 Vector3 newPos = new Vector3(pos.x+shift.x, pos.y+shift.y,pos.z+shift.z);
  8.                
  9.                 TweenPosition tween = TweenPosition.Begin(menu,duration,newPos);
  10.                 //tween.from = pos;
  11.         }

"pos" always returns something like "current x:-0.0001144976,y:0.1177472,z:0" so I'm not sure why menu.transform.position will SHOW the right coordinates in the inspector, but the actual data as revealed by the debug log is wrong. It seems that tweenposition doesnt *actually* move the thing?

EDIT

So I ended up making Shift do this, which seems really backward and obtuse, but if I dont then it won't actually tween from the current position! There has to be a better way. Note that this also only works from a 0,0,0. And since transform doesnt seem to get the actual location, I can't GET the panels location to tween from.

  1. static void Shift (GameObject menu, float duration, Vector3 shift)
  2.         {
  3.                 Vector3 pos;
  4.                 try
  5.                 {
  6.                         pos = menu.GetComponent<TweenPosition>().to;
  7.                 }
  8.                 catch (Exception ex)
  9.                 {
  10.                         pos= Vector3.zero;                     
  11.                 }
  12.                
  13.                 Debug.Log(string.Format("current x:{0},y:{1},z:{2}",pos.x, pos.y, pos.z));
  14.                 Debug.Log(string.Format("Shift x:{0},y:{1},z:{2}",shift.x, shift.y, shift.z));         
  15.                
  16.                 Vector3 newPos = new Vector3(pos.x+shift.x, pos.y+shift.y,pos.z+shift.z);
  17.                
  18.                 TweenPosition tween = TweenPosition.Begin(menu,duration,newPos);
  19.                 tween.from = pos;
  20.         }
  21.  

(also, a secondary question)
Is there any example of a tween's onFinished being used? For the life of me I can't get the thing to disable the object when a tween finishes.
« Last Edit: March 14, 2013, 12:15:17 AM by sintua »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TweenPosition only fires from OLD position!
« Reply #1 on: March 14, 2013, 01:18:08 AM »
For anything more complicated than moving an object from A to B, I really advise creating an Animation instead. You can then use ActiveAnimation.Play to trigger it, and play it in either forward or reverse. You can also specify what happens when the animation finishes playing -- such as disable the object.

That said, TweenPosition.Begin will indeed tween from the current position to your target position. There should be no (0, 0, 0), unless it's your starting position.

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
Re: TweenPosition only fires from OLD position!
« Reply #2 on: March 14, 2013, 01:59:14 AM »
I'ts not that complicated, it's just sliding the panel around. Since it's not one linear predetermined action I can't script an animation for it, but all I need to do is tween the position by a vector from one position to a new one.

So the thing is tween TweenPosition.Begin(menu,duration,newPos) *doesn't* tween from the current position. Every time I run TweenPosition.Begin It always goes back to where it was the first time I ran TweenPosition.Begin. if I start at 100,200,0, TweenPosition.Begin to 100,50,0, then try to TweenPosition.Begin to 200,500,0, it won't start from where the thing *currently* is but instead from where it was the very first time I ran TweenPosition.Begin on it. it's like the *from* value never updates, and since transform.position is apparently returning an entirely different thing I can't get it's actual current position to force "from".

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TweenPosition only fires from OLD position!
« Reply #3 on: March 14, 2013, 04:15:39 AM »
You need to call tween.Reset() if you're reusing an existing tween.

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
Re: TweenPosition only fires from OLD position!
« Reply #4 on: March 14, 2013, 03:05:51 PM »
Thanks for your replies as always!

Oh, I thought TweenPosition.Begin(menu,duration,newPos) just overwrote the existing tween? After the first one the tweenposition sits there disabled (since I can't get the onFinished things to work to destroy it), and I was under the impression that if I ran TweenPosition.Begin(menu,duration,newPos) on the same object, it would tween from its CURRENT position to newPos and disregard whatever tweenPosition component was already on there.

I don't want to reuse the existing tween, I would like to tween to a new position, but whenever I run TweenPosition.Begin(menu,duration,newPos) it will only tween from the original position. Even if I set the "from" explicitly, the final position is still relevant to where it started when i first instantiated the object, and not where it was before running TweenPosition.Begin(menu,duration,newPos).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TweenPosition only fires from OLD position!
« Reply #5 on: March 14, 2013, 04:06:10 PM »
The Begin() call will reuse the tween if it's already there, so you need to Reset() it first.