Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using XInputDotNetPure;
- public class VibrateController : MonoBehaviour
- {
- PlayerIndex playerIndex;
- float vibrationDuration = 1f;
- [Header("Vibration Settings")]
- public bool shouldVibrate; // I think this type of naming suits much better for this type of variable.
- // 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.
- private void Start() => shouldVibrate = true;
- private void Update()
- {
- // if it should not vibrate.
- if (!shouldVibrate)
- {
- SetVibrationOff();
- }
- else
- {
- SetVibrationOn();
- vibrationDuration -= Time.deltaTime;
- if (vibrationDuration <= 0)
- {
- // Just switch off the bool and let logic handle the rest.
- shouldVibrate = false;
- }
- }
- }
- public void SetVibrationOff()
- {
- shouldVibrate = false;
- vibrationDuration = 1f; // You were not setting the duration to its default state.
- GamePad.SetVibration(playerIndex, 0f, 0f);
- }
- public void SetVibrationOn()
- {
- shouldVibrate = true;
- GamePad.SetVibration(playerIndex, 1f, 1f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement