Search Unity

HELP! Automatic animations at certain point!

Discussion in '2D' started by brodan100105, Jan 1, 2017.

  1. brodan100105

    brodan100105

    Joined:
    Jul 16, 2016
    Posts:
    13
    ok, so i made a simple clicker game on android. it is a game where you need to "click the eggs" to get eggs(points) and i want a animation to play when i hit a certain amount of eggs (points;1 million eggs/points) how would i do that, also, i am kinda new to scripting so if you could "dumb" it down for me. that would be great! Thank you! Post any tutorials you think would help also on youtube.
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Have you used Unity's animation system at all? It's kind of easy to do basic stuff. Select the GameObject that you want to animate. Go to the Animator window, press on the Create button and give your new animation a name. If you want to animate between different sprites, select Add Property and Sprite Renderer/Sprite. Now you can drag the sprites you want to use into the timeline to build an animation clip.

    Once you've created some animation clips, you connect them together in the Animator. This is used to control the flow of the animation, which clip will be played when, and how does the flow go from one animation to another. This is generally controlled by using Animation Parameters, these are more or less like ordinary variables, but they are used exclusively by the Animator component.

    So, let's say that you have something like an idle animation to start with. This could be something like a single sprite, that is displayed as long as nothing is happening. This is the Default State, if the animation clip you want to be the default state isn't already set (it is shown as orange), you can right click on it and select Set as Default Layer State.

    Now, to create a transition from one clip to another, right click on one of the clips and select "Make Transition". Now you'll get an arrow which you point to the clip you want to transition to. You'll probably want arrows that in both directions between the clips, so that you can go back and forth between them.

    Finally you need some conditions that are used to trigger the transitions. To start off you need to create some parameters that can be used for this. Go to the Parameters tab and click on the "+". Let's create a bool, name it appropriately. Click on one of your transitions, in the inspector you'll see some information about the transition. At the bottom there is an entry for Conditions. Click on the "+" and select your bool in the drop down. You can now choose if the transition should occur then the bool is true or false. Select the one you think is appropriate. Then do the same on the other transition, but use the opposite for the Condition value.

    You should now be able to test your animation. If you have the Game window and the Animator window open and run your game, you should be able to click on your bool variable in the Animator's Parameter tab. Turning it on and off should trigger your animations.

    Finally, to do this from code... If your script is on the same GameObject that has the animations:
    GetComponent<Animator>().SetBool("NameOfYourBool", true);
    This should trigger your animation...