0
\$\begingroup\$

enter image description hereIn my unity2d game I've create a sprite animation, like a mushsroom bounce.

When I start the game for the first time the animation plays properly, but when the player dies, and retries, the animation won't play. I cannot reload the scene, I have to reset the player's transform.position.

How can I solve this problem?

if (target.tag == "Masroom") {
        target.gameObject.GetComponent<Animator> ().enabled = true;
        playerRigidBody.velocity = new Vector2(0,8);
    }
\$\endgroup\$
1
  • \$\begingroup\$ Can you upload an image of your animation controller? Also, Where is your code sample from? Is it the Update method? \$\endgroup\$
    – hobnob
    Sep 12, 2016 at 12:59

1 Answer 1

2
\$\begingroup\$

You need to do this :

 if (target.tag == "Masroom") {
        target.gameObject.GetComponent<Animator>().Play("MasroomBounce");
        playerRigidBody.velocity = new Vector2(0,8);
    }

What you are doing now is enabling the whole Animator component and that will play only once unless the whole scene is reloaded.

Edit: You have to create a empty state with a transition to your state like this and then the code repeats the animation enter image description here

\$\endgroup\$
8
  • \$\begingroup\$ thanks for reply ,In my game I've cannot be reload scene when player retry I've only reset player transform.position in current scene. . \$\endgroup\$
    – Chetan
    Sep 12, 2016 at 13:12
  • \$\begingroup\$ Yes so use the line of code I provided for you. This will make the Animator play the state with the provided name. I will make a edit \$\endgroup\$
    – Uri Popov
    Sep 12, 2016 at 13:14
  • \$\begingroup\$ @ Uri Popov ok. I try it. \$\endgroup\$
    – Chetan
    Sep 12, 2016 at 13:17
  • \$\begingroup\$ I've try your edited code but still that issue there. \$\endgroup\$
    – Chetan
    Sep 12, 2016 at 13:24
  • \$\begingroup\$ do you get any errors or warnings in the console ? \$\endgroup\$
    – Uri Popov
    Sep 12, 2016 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .