iTween - what the heck is wrong here?

I’m fairly new to Unity, so be easy on me… I’m trying to get a gameObject to tween from a position at the top of the screen to a position on the bottom, then once the tween is complete, move back to the top and do it again.

I’ve added the script to a game object and set the parameters. When I start the game, the gameObject moves to the origin and the tween proceeds to move to the specified destination, just as I expect. When the tween completes, I see the “resetAndPerformAnimation” is called too, however, the gameObject is never repositioned back to origin and subsequently, not re-tweened because apparently, it’s already in the destination.

Why doesn’t this work?

Thanks in advance.

using UnityEngine;
using System.Collections;

public class GamePieceMover : MonoBehaviour
{
public float duration;
public Vector3 origin;
public Vector3 destination;

// Use this for initialization
void Start ()
{
resetAndPerformAnimation();
}

void resetAndPerformAnimation()
{
Debug.Log(“resetAndPerformAnimation”);
gameObject.transform.position = origin;
iTween.MoveTo(gameObject,iTween.Hash(“position”,destination,“time”,duration,“easetype”,iTween.EaseType.linear,“oncomplete”,“resetAndPerformAnimation”));
}

}

Are you sure that iTween is calling resetAndPerformAnimation() again after it completes? Or is the call you’re seeing the one you made from Start()?

Yeah, I’m sure. Interestingly enough, it calls the callback every ‘duration’ seconds. iTween thinks its tweening, but the gameObject never moved back to origin.

This is incredibly odd. I tried a few variation of your approach and it appears there’s some sort of odd timing issue with Unity with regards to setting position. Even adding a delay to the iTween overrides your manual resetting of the position. That being said why not just utilize iTween’s loop:

using UnityEngine;
using System.Collections;

public class GamePieceMover : MonoBehaviour{
	public float duration;
	public Vector3 origin;
	public Vector3 destination;
	
	void Start(){
		transform.position=origin;
iTween.MoveTo(gameObject,iTween.Hash("position",destination,"time",duration,"looptype",iTween.LoopType.loop));
	}
}

I’m not sure looping will help me. This was a simplification of what I was really trying to do. At the end of each animation cycle, I need to randomize the x position in the origin and then begin the cycle again.

It definitely seems timing related. With that thought in mind, I made it delay for one second after resetting the origin and before starting the next tween, but still no luck.

I’m baffled.

Grab the newest version of iTween 2.0.17 and try your initial approach again. iTween was fighting your “oncomplete” callback and should work exactly how you need it to now!

http://itween.pixelplacement.com/index.php

Thanks!

That fixed it. Thank you!