Skip to content

Instantly share code, notes, and snippets.

@pr00thmatic
Created September 20, 2021 20:27
Show Gist options
  • Save pr00thmatic/47d5a499e2d0196fec744d7487acb82e to your computer and use it in GitHub Desktop.
Save pr00thmatic/47d5a499e2d0196fec744d7487acb82e to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
// jugando con el vibrador del gamepad...
// a mi me pasó que la primera vez que intenté usar el vibrador no me funcionó porque Unity dejaba de detectar mi gamepad
// de forma intermitente... uso un control de PS4 que me da montón de problemas!
// para probarlo, dale play, y mueve el stick izquierdo de tu control, debería vibrar intermitentemente...
// si no, intenta detener el juego y volverle a dar play :v
// necesitas el package InputSystem para que funcione (AKA: El Nuevo Input System de Unity).
[RequireComponent(typeof(PlayerInput))]
public class Pr00Rumbler : MonoBehaviour {
// Basado en el video sobre rumbling de Broken Kinghts Games's
// https://www.youtube.com/watch?v=WSw82nKXibc
// edita desde el inspector al quickConfig para probar algunas configuraciones interesantes (quickConfigs)
// ponlo de nuevo en -1 para editar los valores a mano
[Header("Configuration")]
public float speed = 1;
public float waitTime = 0.5f;
public float pulseTime = 0.01f;
public int quickConfig = -1;
[Header("Information")]
public Gamepad gamepad;
public float[][] quickConfigs = new float[][] { // speed, waitTime, pulseTime
new float[] {0.5f, 1.2f, 0.1f}, // latido de corazón normal
new float[] {1, 0.5f, 0.1f} , // latido de corazón frenético
new float[] {0.05f, 0.05f, 0.05f} , // una caja siendo arrastrada
new float[] {0.5f, 0.09f, 0.05f} , // pataditas o_O
new float[] {0.5f, 0.09f, 0.02f} , // MUUUY SUAVECITO (como para asustar al jugador O__O)
new float[] {0.5f, 0.09f, 0.02f} , // constante, regular y mediano
new float[] {1, 0, 1} , // constante, regular y fuerte
new float[] {1, 0.06f, 0.2f} // un tren pasando?
};
[Header("Initialization")]
public PlayerInput input;
void Reset () { input = GetComponent<PlayerInput>(); }
void OnEnable () { Rumble(); }
void OnDisable () { if (GetGamepad() != null) GetGamepad().SetMotorSpeeds(0,0); }
void Update () {
Gamepad found = GetGamepad();
if (found != null) gamepad = found;
if (quickConfig >= 0) {
speed = quickConfigs[quickConfig][0]; waitTime = quickConfigs[quickConfig][1]; pulseTime = quickConfigs[quickConfig][2];
}
}
public void Rumble () { StartCoroutine(_Rumble()); }
IEnumerator _Rumble () {
while (true) {
if (gamepad != null) gamepad.SetMotorSpeeds(speed, speed); yield return new WaitForSeconds(pulseTime);
if (gamepad != null) gamepad.SetMotorSpeeds(0,0); yield return new WaitForSeconds(waitTime);
}
}
public Gamepad GetGamepad () {
return Gamepad.all.FirstOrDefault(g => input.devices.Any(d => d.deviceId == g.deviceId));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment