简体   繁体   中英

How to get animation to play multiple times in Unity?

I want to get a sprite to play an animation of a drop of liquid falling every time that it is clicked, however the animation only plays the first time I click it and I have no idea why.

Here is the code used on the sprite:

public class PipetteScript : MonoBehaviour {

public Animator pipetteAnim;
public BoxCollider2D pipetteMove;
public IndicatorScript indicator;

// Use this for initialization
void Start () {
    pipetteAnim.enabled = true;
    pipetteMove.enabled = true;
    indicator.enabled = true;
}

void OnMouseDown () {
    pipetteAnim.Play ("Pipette_dropping");
    Debug.Log ("Anim playing");
    }
}

The debug log even prints out "Anim playing" every time I click on the sprite.

Use animation in Update function and Let me update

public class PipetteScript : MonoBehaviour {
public Animator pipetteAnim;
public BoxCollider2D pipetteMove;
public IndicatorScript indicator;
public bool boolval = false;

// Use this for initialization
void Start () {
    pipetteAnim.enabled = true;
    pipetteMove.enabled = true;
    indicator.enabled = true;
}
void update()
{
 if(boolval == true)
   pipetteAnim.Play ("Pipette_dropping");
 if(boolval == false)
   pipetteAnim.Stop ("Pipette_dropping");
}
void OnMouseDown () {
    boolval = True;
    }
void OnMouseUp () {
    boolval = False;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM