Making a mobile game where the player moves side to side by tilting the device. Input.acceleration used to work to move the character.
Switching over to the new Accelerometer.current.acceleration.ReadDefaultValue() method does not work for me due to Accelerometer.current returning null.
Does anyone have a solution to this? Below is a snippet of the check I’m using to enable the Accelerometer and to get it’s current value.
private void OnEnable()
{
gameplayInputsActions.Enable();
if (Accelerometer.current != null)
{
InputSystem.EnableDevice(Accelerometer.current);
}
}
private void OnDisable()
{
gameplayInputsActions.Disable();
if (Accelerometer.current != null)
{
InputSystem.DisableDevice(Accelerometer.current);
}
}
private void Update()
{
if (Accelerometer.current != null)
{
Debug.Log(Accelerometer.current.acceleration.ReadValue());
}
ReceiveAxisInput();
//ReceieveTapInput();
}
private void ReceiveAxisInput()
{
switch (currentControlScheme)
{
case ControlScheme.Tilt:
Vector3 acceleration = gameplayInputsActions.Player.Movement.ReadValue<Vector2>();
Vector3 tilt = Accelerometer.current != null ? Accelerometer.current.acceleration.ReadDefaultValue() : Vector3.zero;
Vector3 targetVector = acceleration + tilt;
//Debug.Log(acceleration);
axisInput = targetVector;
break;
case ControlScheme.Drag:
break;
}
//Vector2 keyInput = moveAction;
Vector2 targetInput = axisInput;
axisInput = targetInput;
}
