using UnityEngine; using UnityEngine.XR; public class Rig : MonoBehaviour { public delegate void Ready(); public static event Ready OnReady; [Header("References")] public Design design; public Main main; public GameObject disconnected; public Transform head, hangingRod; public Camera recordCam; [Header("Variables")] public OVRInput.Controller remote; public Quaternion controllerRot = Quaternion.identity; public Quaternion inputRot = Quaternion.identity; public Vector3 controllerPos, joystick; bool leftOn, rightOn, touchOn; Vector3 headOffset; public Vector3 boxShake, boxOffset; public bool ready, alignRecordCam; void Start() { Game.OnBump += Bump; Game.OnCrash += Bump; // headOffset.y = 32f; headOffset.y = design.headOffset.y; headOffset.z = design.headOffset.z; ready = false; recordCam.gameObject.SetActive(Application.isEditor); } void Update() { // Input System UnityEngine.XR.XRDevice.DisableAutoXRCameraTracking(Camera.main, true); leftOn = OVRInput.IsControllerConnected(OVRInput.Controller.LTrackedRemote); rightOn = OVRInput.IsControllerConnected(OVRInput.Controller.RTrackedRemote); touchOn = OVRInput.IsControllerConnected(OVRInput.Controller.RTouch); if (leftOn || rightOn || touchOn) { if (leftOn) { remote = OVRInput.Controller.LTrackedRemote; } else if (rightOn) { remote = OVRInput.Controller.RTrackedRemote; } else if (touchOn) { remote = OVRInput.Controller.RTouch; } disconnected.SetActive(false); Time.timeScale = 1; } else { disconnected.SetActive(true); Time.timeScale = 0; } // Rise Up Transition if (!ready && headOffset.y == design.headOffset.y) { OnReady(); ready = true; } else { headOffset.y = Mathf.Lerp(headOffset.y, design.headOffset.y, Time.deltaTime); headOffset.z = Mathf.Lerp(headOffset.z, design.headOffset.z, Time.deltaTime); } Quaternion headsetRot = InputTracking.GetLocalRotation(XRNode.Head); if (headsetRot != null) { head.rotation = headsetRot; // Quaternion.LookRotation(head.forward, head.up); hangingRod.position = design.pivotPos; hangingRod.rotation = headsetRot; head.position = design.pivotPos + (headsetRot * headOffset); head.localScale = Vector3.one * design.scale; } boxShake = Vector3.Lerp(boxShake, (boxShake - boxOffset) * 0.5f, design.boxSpring * Time.deltaTime); boxOffset += boxShake * design.boxSpring * Time.deltaTime; head.position += boxOffset; // Record Cam Vector3 twoDir = new Vector3(head.position.x, 0, head.position.z).normalized; Vector3 recPos = (Quaternion.LookRotation(twoDir) * new Vector3(0.5f, 0.25f, 0.5f).normalized * 40); if (alignRecordCam) { recordCam.transform.position = recPos; recordCam.transform.rotation = Quaternion.LookRotation(-recPos); } // controllerRot = OVRInput.GetLocalControllerRotation(remote) * Quaternion.Euler(-30, 0, 0); Vector3 headsetPos = InputTracking.GetLocalPosition(XRNode.Head); controllerPos = OVRInput.GetLocalControllerPosition(remote) * design.scale; controllerPos = controllerPos - headsetPos * design.scale; controllerPos += head.position; inputRot = controllerRot = OVRInput.GetLocalControllerRotation(remote); joystick = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick, remote); joystick = new Vector3(joystick.x, 0, joystick.y); if (joystick.sqrMagnitude > 0) { inputRot *= Quaternion.LookRotation(joystick); } } public Vector3Int InputDirection(Main main) { Vector3Int newDir = Main.SnapDir((inputRot * Vector3.forward)); // no neck snaps if (main.snake[0] + newDir != main.snake[1]) { return newDir; } return main.dir; } void Bump(Vector3 dir) { boxShake -= dir; } }