using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
public class JoystickController : MonoBehaviour
{
// InputActionReference for the joystick movement (Vector2)
public InputActionReference joystickActionReference;
// UnityEvents for the horizontal and vertical axis
public UnityEvent<float> onHorizontalMovement;
public UnityEvent<float> onVerticalMovement;
private InputAction joystickAction;
private void Awake()
{
// Reference the actual input action from the InputActionReference
joystickAction = joystickActionReference.action;
}
private void OnEnable()
{
// Enable the action
joystickAction.Enable();
// Subscribe to the input action's performed callback
joystickAction.performed += OnJoystickMove;
}
private void OnDisable()
{
// Disable the action
joystickAction.Disable();
// Unsubscribe from the performed callback to prevent memory leaks
joystickAction.performed -= OnJoystickMove;
}
private void OnJoystickMove(InputAction.CallbackContext context)
{
// Get the Vector2 value from the joystick
Vector2 joystickValue = context.ReadValue<Vector2>();
// Invoke the UnityEvents with the horizontal (x) and vertical (y) values
onHorizontalMovement.Invoke(joystickValue.x);
onVerticalMovement.Invoke(joystickValue.y);
}
}
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.InputActionReference.html
Source: https://chatgpt.com/share/66f1c0ce-7c84-800e-87ba-1f9548147e96