tiistai 18. lokakuuta 2016

Unity platformer - Animate float

NAV15AP

2. jakso lukuvuonna 2016-2017

Topic for this tutorial is a single action called animate float. It’s versatile and handy action that has many different uses. In this tutorial you’re going to cover three ways to use it.

ANIMATING SCALE

First way to use it is to scale objects. For example, when collecting objects, you can use it to scale down the collectible until it’s 0 and then destroying the object. Create FSM and box collider for the collectible. Make the collider trigger by checking the is trigger option in the box collider. In the start state use action trigger 2D event. Select the player as the collide tag and name event you want to send.


The second state has the animate float and set scale action. For this you’ll need float variable to scale.


In the Anim curve section you’ll need to set curve which controls the animation. Numbers at the bottom represent time. Choose the length you want your animation to be by dragging the curve to the desired position. Numbers on the left are the variable you’re using. Drag the curve to the number one so that the scaling starts from the normal size of the object. When dragging the curve select it with mouse and then hold ctrl while dragging. This makes the curve move evenly. 


Use your float variable in the float variable section and send finished event after the scaling is done. The set scale action uses your float variable in every axis. Remember to check the every frame option so that the scaling will happen in real time.


In the last state use destroy self action. This will destroy the object after the animated scaling has finished.

ANIMATING HEALTH BAR

This one is a bit more tricky to accomplish but it’s still an easy way to animate your health bar without using a spritesheet.

First you need the health bar done with NGUI as advised in one of the previous tutorials. Create FSM for the health bar. 


You’ll need and idle start state that has set property action. Remember to drag the UI sprite to the state so that you can set property for that sprite. Choose fillamount as property and your global health variable as the value.


Then make another state. This state need global transition which is sent from the player get hit FSM with send event by name. In this state use int switch action. Select number of switches you need. In this case the number is 4 because every time player is hit 0.3 is taken from health. Then you need 4 events. Link every event to correct switch. When using health be sure to make the number in the less than section a bit bigger than the actual health will be. If it’s exactly the same then it won’t be recognized properly. Start with the smallest amount and move towards the biggest so that Playmaker does the action correctly.


The last four states are quite similar. Only difference will be in the curve. The curve in each must correspond to the amount of health you want to animate in that state. For example if you want to animate health from 0.7 to 0.4 then you need to select those values for the curve. Do the curves for each state and link those with FINISHED event to the start state.




CHANGING MUSIC VOLUME

Lastly you’re going to learn how to change music volume with animate float. Best use for this is when you want music to fade in/out when starting or exiting a scene. This tutorial is for scaling music up when starting game.

First you’ll need to import some music to the game and use it as audio source. 


Create FSM for the music. There are only two states needed in the FSM.


First state will have two actions. First set property for the audio source component. This can be done by dragging the audio source to the start state. Select volume as property and create a float variable for soun and use it in the set value section. Remember to put check mark in the every frame option so that the volume will scale up smoothly. Second action is animate float. Vertically start point is 0 and end point is 1 because zero is no no volume and 1 is full volume. Horizontally you’ll need to decide how long you want the music to use in scaling up. Select your sound as the float variable and use FINISHED as the finish event.



The next state will have only one action called finish FSM. This action will simply finish the FSM so that it won’t be running in the background.


Ei kommentteja:

Lähetä kommentti