Advertisement
Jon50

VibrateController

Feb 27th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using UnityEngine;
  2. using XInputDotNetPure;
  3.  
  4. public class VibrateController : MonoBehaviour
  5. {
  6.     PlayerIndex playerIndex;
  7.     float vibrationDuration = 1f;
  8.  
  9.     [Header("Vibration Settings")]
  10.     public bool shouldVibrate; // I think this type of naming suits much better for this type of variable.
  11.  
  12.     // Use the Start method to set the default state, the turn on and off logic should be self sustained, if that is not the case, your logic is flawed.
  13.     private void Start() => shouldVibrate = true;
  14.  
  15.     private void Update()
  16.     {
  17.         // if it should not vibrate.
  18.         if (!shouldVibrate)
  19.         {
  20.             SetVibrationOff();
  21.         }
  22.         else
  23.         {
  24.             SetVibrationOn();
  25.             vibrationDuration -= Time.deltaTime;
  26.             if (vibrationDuration <= 0)
  27.             {
  28.                 // Just switch off the bool and let logic handle the rest.
  29.                 shouldVibrate = false;
  30.             }
  31.         }
  32.     }
  33.  
  34.     public void SetVibrationOff()
  35.     {
  36.         shouldVibrate = false;
  37.         vibrationDuration = 1f; // You were not setting the duration to its default state.
  38.         GamePad.SetVibration(playerIndex, 0f, 0f);
  39.     }
  40.  
  41.     public void SetVibrationOn()
  42.     {
  43.         shouldVibrate = true;
  44.         GamePad.SetVibration(playerIndex, 1f, 1f);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement