This repository has been archived on 2024-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
snakeinabox/Assets/Scripts/Rig.cs
2020-07-09 11:41:01 -07:00

105 lines
No EOL
2.5 KiB
C#

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;
[Header("Variables")]
public OVRInput.Controller remote;
public Quaternion controllerRot = Quaternion.identity;
bool leftOn, rightOn, touchOn;
Vector3 headOffset;
public Vector3 boxShake, boxOffset;
public bool ready;
void Start()
{
Game.OnBump += Bump;
headOffset.y = 32f;
headOffset.z = design.headOffset.z;
ready = false;
}
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;
head.position = 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;
controllerRot = OVRInput.GetLocalControllerRotation(remote) * Quaternion.Euler(-30, 0, 0);
}
public Vector3Int InputDirection(Main main)
{
Vector3Int newDir = Main.SnapDir((controllerRot * Vector3.forward));
// no neck snaps
if (main.snake[0] + newDir != main.snake[1])
{
return newDir;
}
return main.dir;
}
void Bump(Vector3 dir)
{
boxShake -= dir;
}
}