Skip to content

Instantly share code, notes, and snippets.

@pr00thmatic
Last active December 10, 2021 23:46
Show Gist options
  • Save pr00thmatic/9998289c696565acb4e207a7b1b15204 to your computer and use it in GitHub Desktop.
Save pr00thmatic/9998289c696565acb4e207a7b1b15204 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.XR;
using System.Collections;
using System.Collections.Generic;
// en la documentación de Unity XRInput podrás leer más sobre
// los comandos usados en este script
// --> https://docs.unity3d.com/Manual/xr_input.html
public class OculusControllerExample : MonoBehaviour {
[Header("Configuration")]
public float speed = 2;
[Header("Information")]
public InputDevice left;
public InputDevice right;
[Header("Initialization")]
public GameObject rig;
void Start () {
List<InputDevice> found = new List<InputDevice>();
// encontremos la izquierda...
// mira la sección "Accessing input devices by characteristics"
// Usamos "|" y no "||" porque realizamos una operación binaria entre esos dos valores (que son de tipo int)
// y no una operación booleana que requeriría que ambos valores sean de tipo bool.
// 2^1 en binario es 10, 2^2 en binario es 100,
// así que 2 | 4 = 6 (en binario: "10 | 100 = 110")
// En este caso, es una forma de almacenar datos usando sólamente una variable de tipo int.
// Se llama "Bitmask"
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Left, found);
left = found[0]; // solo un controller cumple ese criterio en el Oculus
found.Clear();
// y ahora la derecha...
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Right, found);
right = found[0];
}
void Update () {
// revisa qué tipo de dato devuelve en la documentación!
// 2D Axis es un Vector2: value.x para horizontal, value.y para vertical
// Axis es un float
// Button es un bool
// el trigger que presionas con el índice es un Axis!
// el stick es un 2D Axis!
// el botón B es un Button!
Vector2 value;
// revisa cuáles acciones están disponibles en la sección XR input mappings
right.TryGetFeatureValue(CommonUsages.primary2DAxis, out value);
Debug.Log(value.x + ", " + value.y);
// este es un ejemplo de cómo mover al jugador alrededor usando el stick del control derecho
Vector3 deltaPos = (rig.transform.forward * value.y + rig.transform.right * value.x) * speed * Time.deltaTime;
deltaPos.y = 0;
transform.position += deltaPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment