Search code examples
c#unity-game-engineitween

Unity3D: iTween oncomplete not working


I am working on a Unity2 game, and I want to make Boom disappear in the end of iTween animation.

Here is part of code

void OnMouseDown() {    

    if (ChosePosition == false)
        ChosePosition = true;

    // make Star stop
    else if (ChosePosition == true && ChoseForce == false) {
        ChoseForce = true;
        //Compute Force power 
        PowerDegree = Mathf.Abs (Star.transform.position.y - StarPosition)/ Mathf.Abs(StarendPosition - StarPosition) * 100;

        //print (PowerDegree);

        Vector3 NewBoomPostion = new Vector3 (Luncher.transform.position.x, BoomPosition, 85);

        GameObject newBoom = Instantiate(Boom, NewBoomPostion , Quaternion.identity) as GameObject;
        iTween.MoveTo (newBoom, iTween.Hash 
            ("y",BoomPosition+((BoomendPosition-BoomPosition)/100*PowerDegree),
            "speed",Boomspeed,
            "EaseType",BoomeaseType,
            "LoopType",BoomloopType,
            "oncomplete","BoomComplete"
            ));


        CarrierReset = true;
        ChosePosition  = false;
        ChoseForce = false;

        //After Shot, Luncher will move
        iTween.Resume(Luncher);

        //After Shot, Luncher Carrier will display
        displayCarrierOrNot = true;
    }
}


void BoomComplete(){
    print("complete");
}

But I can't see "complete" in console.


Solution

  • This post on Unity Answers describes why you have the problem:

    By default iTween attempts to call the callback methods you provide it on the object it is animating - in your case the mainCamera.gameObject. Since "onCameraShakeComplete" does not reside on that object it is never getting called. You have two options: Move that method onto your mainCamera.gameObject or simply provide am "onCompleteTarget" of gameObject to tell iTween to use the GameObject that is setting up this iTween.

    In your case the object it is animating is newBoom, but you have the callback on your own component. To make iTween use your current callback you can add a parameter to your Hash call:

    iTween.MoveTo (newBoom, iTween.Hash(
        // ...
        "oncomplete", "BoomComplete",
        "oncompletetarget", gameObject
        ));