Search Unity

Question Why is SetMotorSpeeds not working ?

Discussion in 'Input System' started by Loufouh, Apr 5, 2023.

  1. Loufouh

    Loufouh

    Joined:
    Jul 6, 2022
    Posts:
    10
    Hello,

    I'm trying to make my controller rumble, I tried with a Stadia and a PS3 controller but it's doing nothing...
    I'm looking for the solution for several days, even asked to ChatGPT, it told me about activating vibrations in project settings but I can't find this option so...

    My code is below, it is simply setting Gamepad.current to the variable _gamepad, and calling SetMotorSpeeds() with the value of the right trigger (and some GUI debug).

    Everything works as expected but the controller does not vibrate oO

    Thanks for reading me!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. namespace Player
    5. {
    6.     [RequireComponent(typeof(PlayerInput))]
    7.     public class Vibrate : MonoBehaviour
    8.     {
    9.         private Gamepad _gamepad;
    10.         private float _leftMotorSpeed;
    11.         private float _rightMotorSpeed;
    12.        
    13.         private PlayerInput _playerInput;
    14.         private InputAction _vibrateAction;
    15.  
    16.         void Awake()
    17.         {
    18.             _playerInput = GetComponent<PlayerInput>();
    19.         }
    20.  
    21.         void Start()
    22.         {
    23.             _gamepad = Gamepad.current;
    24.            
    25.             _vibrateAction = _playerInput.actions["vibrate"];
    26.             _vibrateAction.performed += PerformVibrateAction;
    27.             _vibrateAction.canceled += CancelVibrateAction;
    28.         }
    29.  
    30.         void OnGUI()
    31.         {
    32.             GUI.Label(new Rect(0, 0, 300, 20), $"{_gamepad}");
    33.             GUI.Label(new Rect(0, 20, 300, 20), $"Left Motor : {_leftMotorSpeed}");
    34.             GUI.Label(new Rect(0, 40, 300, 20), $"Right Motor : {_rightMotorSpeed}");
    35.         }
    36.  
    37.        
    38.         private void PerformVibrateAction(InputAction.CallbackContext ctx)
    39.         {
    40.             float value = ctx.ReadValue<float>();
    41.             SetMotorSpeeds(value, value);
    42.         }
    43.        
    44.         private void CancelVibrateAction(InputAction.CallbackContext ctx)
    45.         {
    46.             SetMotorSpeeds(0f, 0f);
    47.         }
    48.  
    49.         private void SetMotorSpeeds(float leftSpeed, float rightSpeed)
    50.         {
    51.             _leftMotorSpeed = leftSpeed;
    52.             _rightMotorSpeed = rightSpeed;
    53.            
    54.             _gamepad.SetMotorSpeeds(leftSpeed, rightSpeed);
    55.         }
    56.     }
    57. }
     
  2. Loufouh

    Loufouh

    Joined:
    Jul 6, 2022
    Posts:
    10
    Am I the only person having this problem right now?
    The problem happens with my two computers, and it doesn't change anything after building the game and distributing it to other people.
     
  3. Arganoid

    Arganoid

    Joined:
    Oct 12, 2013
    Posts:
    24
    I don't know, but I'm having a problem where rumble works inconsistently when the frame rate is very high, so I'm curious whether you have a very high frame rate when you're testing this. I'm going to try putting the SetMotorSpeeds call in FixedUpdate so it runs less often.
     
  4. Loufouh

    Loufouh

    Joined:
    Jul 6, 2022
    Posts:
    10
    Thanks for the post,
    Yes, my framerate is around 820 FPS, but because SetMotorSpeeds is called only on the controller event I don't see how it could be related
     
  5. Loufouh

    Loufouh

    Joined:
    Jul 6, 2022
    Posts:
    10
    Hello,
    just tried with a Nintendo Switch Pro Controller and the issue persists.
    Am I still the only human with such a problem right now ?
     
    montymomentum_unity likes this.
  6. blingopen

    blingopen

    Joined:
    Feb 4, 2018
    Posts:
    2
    Maybe you haven't read Known Limitations in the Input System manual.
    gamepad
    only works well on xbox or PS4. https://docs.unity3d.com/Packages/com.unity.inputsystem@1.6/manual/KnownLimitations.html
    • (Stadia) The Stadia controller is only supported in the Stadia player at the moment. In the editor, use the generic Gamepad for bindings and use any Xbox or PS4 controller for testing.
    • Joy-Cons are only supported on Switch.
    • Sensors in the PS4 controller are currently only supported on PS4.
    And Also Nintendo Switch Pro Controller is not supported on Windows. https://docs.unity3d.com/Packages/com.unity.inputsystem@1.6/manual/Gamepad.html

    Note: Only the following combinations of Devices/OSes currently support rumble:

    • PS4, Xbox, and Switch controllers, when connected to their respective consoles. Only supported if you install console-specific input packages in your Project.
    • PS4 controllers, when connected to Mac or Windows/UWP computers.
    • Xbox controllers on Windows.
     
  7. Loufouh

    Loufouh

    Joined:
    Jul 6, 2022
    Posts:
    10
    Thanks a lot blingopen,
    You're right, I didn't check this section out, sorry...
    I'm quite surprised that the rumble function is not supported on a lot more controllers