|
| 1 | +# Converting A Float Action To A Boolean Action |
| 2 | + |
| 3 | +> * Level: Beginner |
| 4 | +> |
| 5 | +> * Reading Time: 5 minutes |
| 6 | +> |
| 7 | +> * Checked with: Unity 2018.3.14f1 |
| 8 | +
|
| 9 | +## Introduction |
| 10 | + |
| 11 | +Sometimes you may want to use the values from a `FloatAction` to activate or deactivate an action type. For example, pressing down the trigger axis on a controller causes an action to occur when the trigger is fully pressed down. |
| 12 | + |
| 13 | +The `Input Unity Input Manager Axis Actions` provide a way of getting the varying value data from an axis but don’t really provide a way of letting us trigger something when that value reaches a specific value. This can be done by taking the float value and converting it into a boolean when the float value is within our specified bounds. We can then take this boolean value and pass it into a `Boolean Action` for hooking up events that are either on or off (true/false). |
| 14 | + |
| 15 | +We can do this using the Data Type Converters provided with [Zinnia.Unity] which allow conversions from one DataType to another. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +* [Install the Tilia.Input.UnityInputManager] package dependency in to your [Unity] project. |
| 20 | + |
| 21 | +## Let's Start |
| 22 | + |
| 23 | +### Step 1 |
| 24 | + |
| 25 | +Create a new `Cube` Unity 3D Object by selecting `Main Menu -> GameObject -> 3D Object -> Cube` and change the `Transform` properties to: |
| 26 | + |
| 27 | +* Position: `X = 0`, `Y = 0`, `Z = 0` |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +### Step 2 |
| 32 | + |
| 33 | +Expand the `Tilia Input UnityInputManager` Package directory in the Unity Project window and select then `Packages -> Tilia Input UnityInputManager -> Runtime -> Prefabs -> Actions` directory then drag and drop the `Input.UnityInputManager.Axis1DAction` prefab into the Hierarchy window. |
| 34 | + |
| 35 | +> Be sure to use the `Input.UnityInputManager.Axis1DAction` prefab and not just add the `Unity Input Manager Axis 1D Action` component to a GameObject as the component needs linking to a `MomentProcessor` to function. |
| 36 | +
|
| 37 | + |
| 38 | + |
| 39 | +### Step 3 |
| 40 | + |
| 41 | +Select the `Input.UnityInputManager.Axis1DAction` GameObject in the Hierarchy window and change the `Axis Name` parameter on the `Unity Input Manager Axis 1D Action` component to listen for the desired axis, in this case enter `Tilia.Input.UnityInputManager_Axis9` to listen for the Left Trigger axis on an Xbox Controller. |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +### Step 4 |
| 46 | + |
| 47 | +Create an empty `GameObject` in the Unity Hierarchy then click the `Add Component` button and select the `Boolean Action` component. This newly created `Boolean Action` will be the action that defines when our trigger axis is true or false. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +### Step 5 |
| 52 | + |
| 53 | +Click the `+` symbol in the bottom right corner of the `Activated` event parameter in the `Boolean Action` component and then drag and drop the `Cube` GameObject into the box that appears and displays `None (Object)`. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +Select a `Function` to perform when the `Activated` event is emitted. For this example, select `GameObject -> SetActive(bool)` (be sure not to select `Dynamic bool - SetActive` for this example). |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +### Step 6 |
| 62 | + |
| 63 | +Do the same as Step 5 but for the `Deactivated` event parameter in the Boolean Action component, but tick the checkbox under the `GameObject.SetActive` function. |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +### Step 7 |
| 68 | + |
| 69 | +Click the `Add Component` button and select the `Float To Boolean` component. This newly created `Float To Boolean` will convert our float value from the trigger axis to a boolean when the value is within our defined range. |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +### Step 8 |
| 74 | + |
| 75 | +Click the `+` symbol in the bottom right corner of the `Transformed` event parameter in the `Float To Boolean` component and then drag and drop the `Boolean Action` component into the box that appears and displays `None (Object)`. |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +Select a `Function` to perform when the `Transformed` event is emitted. For this example, select `Boolean Action -> Receive` (be sure to select `Dynamic bool - Receive` for this example). |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +### Step 9 |
| 84 | + |
| 85 | +Set the `Positive Bounds` values to `0.5` and `1` on the `Float To Boolean` component. |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +### Step 10 |
| 90 | + |
| 91 | +Select the `Input.UnityInputManager.1DAxisAction` in the Unity Hierarchy and click the `+` symbol in the bottom right corner of the `Value Changed` event parameter in the `Unity Input Manager Axis 1D Action` component and then drag and drop the `GameObject` GameObject into the box that appears and displays `None (Object)`. |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +Select a `Function` to perform when the `Value Changed` event is emitted. For this example, select `Float To Boolean -> DoTransform` (be sure to select `Dynamic float - DoTransform` for this example). |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +### Done |
| 100 | + |
| 101 | +Play the Unity scene and press the Left Trigger axis on the Xbox Controller and notice how the Cube will disappear when the axis passes half way. This Boolean Action can now be used to drive other output events too such as grabbing an item or even activating a pointer. |
| 102 | + |
| 103 | +[Install the Tilia.Input.UnityInputManager]: ../Installation/README.md |
| 104 | +[Unity]: https://unity3d.com/ |
0 commit comments