3 – AnalogInputEvent component
AnalogInputEvent listens to one analog Input Action and fires a UnityEvent<float> every time the value changes. The value runs from 0 to 1, whatever the board’s resolution.
In this tutorial a potentiometer on A0 controls the brightness of a spotlight.
Before you start
Section titled “Before you start”- Wire the demo circuit, with the potentiometer on A0.
- Create a profile and Input Actions with A0 set to Analog In, and add the SerialInputBridge prefab to the scene.
1. Add the component
Section titled “1. Add the component”- Create an empty GameObject, for example
Knob. - Add Component > search AnalogInputEvent.
- In Action Reference, pick the A0 action from your generated Input Actions asset.
The component enables the action for you. You don’t need a PlayerInput component.
2. Hook up something to control
Section titled “2. Hook up something to control”The package includes a small example script, LightMultiplier, in Assets/SerialInputBridge/fromtutorialvideos/. It sets a light’s intensity to value × multiplier.
- Add a Spot Light to the scene.
- Add the LightMultiplier component to any GameObject and drag the spotlight into Spot Light. Leave Multiplier at 100 or adjust to taste.
- Back on the AnalogInputEvent, click + under On Value Changed.
- Drag the GameObject with LightMultiplier into the slot and choose LightMultiplier > SetIntensity from the Dynamic float section at the top of the list.
3. Press Play
Section titled “3. Press Play”Turn the knob. The spotlight dims and brightens.
Using the value in your own script
Section titled “Using the value in your own script”Any public method with a single float parameter shows up under Dynamic float:
using UnityEngine;
public class RotateWithKnob : MonoBehaviour{ public float maxAngle = 270f;
// Hook to AnalogInputEvent > On Value Changed (Dynamic float) public void SetValue(float value) { transform.localRotation = Quaternion.Euler(0f, value * maxAngle, 0f); }}