User Tools

Site Tools


public:t-vien-15-3:lab5_it_moves

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

public:t-vien-15-3:lab5_it_moves [2015/09/18 09:06]
hannes
public:t-vien-15-3:lab5_it_moves [2015/09/18 09:06] (current)
hannes [Procedure]
Line 24: Line 24:
   - **Making a Conditional Transition** By default, a transition happens when an animation clip has reached its end. But transitions can also be tied to other conditions. Select the transition from the first animation to the second. Locate the **Conditions** sub-panel in the **Inspector**. There are not many choices there at this time, you will need to create a new state machine parameter to give you more choices. Each new state machine parameter can be used in a condition. There are different types of such parameters, including float values (e.g. modeling a psychological state) and booleans (e.g. modeling a toggle between to modes). We will create a trigger parameter, which in a way behaves like a boolean but does not remain in a true state. It is simply a one-shot "true" event. Using such a parameter as a transition trigger ensures that we only follow the transition once whenever we set this parameter. Locate the **Parameters** panel in the **Animator** view. Press the **+** symbol and select **Trigger**. Give the trigger parameter a name like ''UserPresent''. Then back in the **Conditions** sub-panel of the transition from the first to the second animation, you should now select this parameter.    - **Making a Conditional Transition** By default, a transition happens when an animation clip has reached its end. But transitions can also be tied to other conditions. Select the transition from the first animation to the second. Locate the **Conditions** sub-panel in the **Inspector**. There are not many choices there at this time, you will need to create a new state machine parameter to give you more choices. Each new state machine parameter can be used in a condition. There are different types of such parameters, including float values (e.g. modeling a psychological state) and booleans (e.g. modeling a toggle between to modes). We will create a trigger parameter, which in a way behaves like a boolean but does not remain in a true state. It is simply a one-shot "true" event. Using such a parameter as a transition trigger ensures that we only follow the transition once whenever we set this parameter. Locate the **Parameters** panel in the **Animator** view. Press the **+** symbol and select **Trigger**. Give the trigger parameter a name like ''UserPresent''. Then back in the **Conditions** sub-panel of the transition from the first to the second animation, you should now select this parameter. 
   - ** Trigger Transition from Script ** Your newly created animation state machine parameter can now be set from any Unity script, and thus you can affect the state of animation. Back in the regular scene, locate your character game object and add a ''Collision Sphere'' to it, make it a ''Trigger'' and adjust its radius so that it will trigger at a distance about half the way between you (the first person controller) and the character. Add a new scrip to the character that intercepts the trigger event (''void OnTriggerEnter(Collider col)'') and **set the UserPresent trigger parameter** in the animator controller. You do that by first acquiring a reference to the ''Animator'' component (hint: Use ''GetComponent''). Then on that component you call ''SetTrigger("UserPresent")''. Check out the [[https://docs.unity3d.com/Documentation/ScriptReference/Animator.SetTrigger.html|documentation for ''SetTrigger'']]. If everything is working properly, you should now see the transition from the first to second animation when you walk towards the character. Make sure that the second animation only plays once and then returns to the first animation (until you cross the trigger boundary again).    - ** Trigger Transition from Script ** Your newly created animation state machine parameter can now be set from any Unity script, and thus you can affect the state of animation. Back in the regular scene, locate your character game object and add a ''Collision Sphere'' to it, make it a ''Trigger'' and adjust its radius so that it will trigger at a distance about half the way between you (the first person controller) and the character. Add a new scrip to the character that intercepts the trigger event (''void OnTriggerEnter(Collider col)'') and **set the UserPresent trigger parameter** in the animator controller. You do that by first acquiring a reference to the ''Animator'' component (hint: Use ''GetComponent''). Then on that component you call ''SetTrigger("UserPresent")''. Check out the [[https://docs.unity3d.com/Documentation/ScriptReference/Animator.SetTrigger.html|documentation for ''SetTrigger'']]. If everything is working properly, you should now see the transition from the first to second animation when you walk towards the character. Make sure that the second animation only plays once and then returns to the first animation (until you cross the trigger boundary again). 
-  - **MORE REACTIVE** You may have noticed that the second animation does not start immediately when the trigger event occurs. The first animation gets to finish its loop, before the transition takes place. This is not very reactive. In order to force the transition before the first animation gets to finish, you have to uncheck the ''Has Exit Time'' option in the transition (select the transition arrow and find this parameter in the **Inspector**). +  - **More Reactive** You may have noticed that the second animation does not start immediately when the trigger event occurs. The first animation gets to finish its loop, before the transition takes place. This is not very reactive. In order to force the transition before the first animation gets to finish, you have to uncheck the ''Has Exit Time'' option in the transition (select the transition arrow and find this parameter in the **Inspector**). 
   - **OPTIONAL: Optimize** Searching for the ''Animator'' component every time you want to access its ''SetTrigger'' method is not fast. Also, it is not particularly fast to set the trigger by name, i.e. the state machine has to search for the matching string for that parameter before setting its value. Instead you can pre-calcualte the hash ID for this parameter and pass that id into the function (see the [[https://unity3d.com/learn/tutorials/projects/stealth/hashids|Hash IDs Tutorial]]). So, to optimise do two things: Store a reference to the animator component AND a hash id value for the parameter you want to change as member variables in your behavior script. Only assign values to these two variables once.    - **OPTIONAL: Optimize** Searching for the ''Animator'' component every time you want to access its ''SetTrigger'' method is not fast. Also, it is not particularly fast to set the trigger by name, i.e. the state machine has to search for the matching string for that parameter before setting its value. Instead you can pre-calcualte the hash ID for this parameter and pass that id into the function (see the [[https://unity3d.com/learn/tutorials/projects/stealth/hashids|Hash IDs Tutorial]]). So, to optimise do two things: Store a reference to the animator component AND a hash id value for the parameter you want to change as member variables in your behavior script. Only assign values to these two variables once. 
/var/www/ailab/WWW/wiki/data/pages/public/t-vien-15-3/lab5_it_moves.txt ยท Last modified: 2015/09/18 09:06 by hannes