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

205 lines
5.6 KiB
C#

using System;
using UnityEngine;
using UnityEngine.XR;
[Serializable]
public class Rig : MonoBehaviour
{
[HideInInspector]
public Monolith mono;
[Header("References")]
public Design design;
public Main main;
public GameObject disconnected;
public Transform head, con, lCon, hangingRod;
public Camera recordCam;
[Header("Variables")]
public OVRInput.Controller ovrCon;
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 alignRecordCam;
void Start()
{
Game.OnBump += Bump;
Game.OnCrash += Bump;
// headOffset.y = 32f;
headOffset.y = design.headOffset.y;
headOffset.z = design.headOffset.z;
recordCam.gameObject.SetActive(Application.isEditor);
}
Quaternion flipRot = Quaternion.identity;
float flipEuler = 180;
public Lerper lerper;
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) { ovrCon = OVRInput.Controller.LTrackedRemote; }
else if (rightOn) { ovrCon = OVRInput.Controller.RTrackedRemote; }
else if (touchOn) { ovrCon = OVRInput.Controller.RTouch; }
disconnected.SetActive(false);
Time.timeScale = 1;
}
else
{
disconnected.SetActive(true);
Time.timeScale = 0;
}
// Apply to Transforms
Quaternion flip = Quaternion.identity;
head.localPosition = InputTracking.GetLocalPosition(XRNode.Head);
head.localRotation = InputTracking.GetLocalRotation(XRNode.Head);
con.localPosition = InputTracking.GetLocalPosition(XRNode.RightHand);
con.localRotation = InputTracking.GetLocalRotation(XRNode.RightHand);
lCon.localPosition = InputTracking.GetLocalPosition(XRNode.LeftHand);
lCon.localRotation = InputTracking.GetLocalRotation(XRNode.LeftHand);
// °inview
if (OVRInput.GetDown(OVRInput.Button.PrimaryHandTrigger, ovrCon))
{
flipEuler *= -1;
lerper.t *= -1;
}
transform.localScale = Vector3.one * design.scale;
Quaternion targetRot = Quaternion.Euler(0, flipEuler, 0);
if (OVRInput.Get(OVRInput.Button.PrimaryHandTrigger, ovrCon))
{
lerper.Update(1);
}
else
{
lerper.Update(0);
}
flipRot = Quaternion.SlerpUnclamped(Quaternion.identity, targetRot, lerper.t);
transform.rotation = flipRot;
transform.position = flipRot * -head.localPosition * design.scale;
transform.position += design.pivotPos + (head.rotation * headOffset);
// Shake
boxShake = Vector3.Lerp(boxShake, (boxShake - boxOffset) * 0.5f, design.boxSpring * Time.deltaTime);
boxOffset += boxShake * design.boxSpring * Time.deltaTime;
transform.position += boxOffset;
// Hanging Rod
hangingRod.position = design.pivotPos;
hangingRod.rotation = head.rotation;
// 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);
}
// °fullrot
// inputRot = controllerRot = OVRInput.GetLocalControllerRotation(remote);
// point & swipe swap/choose
// both hands? (nah makes point the odd one out)
// HOW TO FILTER OUT THE SPIN CHAIR VEL? (LCON? NAH... BUT MIGHT BE GOOD FOR TESTINGs)
// flip view? what about 90/45 degree snaps ( or drag view!? )
// all oculus bs pulled filtered through a single file
// menu system cull
// PRIORITY
// starting scene! the sooner the better ( break things! )
// refactor like vader life alyx!
// °dragdir
Vector3 conVel = flipRot * (con.localPosition - oldConPos) / Time.deltaTime;
OVRInput.SetControllerVibration(0, 0, OVRInput.Controller.RTouch);
if (OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger, ovrCon))
{
if (conVel.magnitude > 0.05f)
{
OVRInput.SetControllerVibration(1, 0.1f, OVRInput.Controller.RTouch);
}
rawDir = (rawDir + conVel * Time.deltaTime * 30).normalized;
}
inputRot = Quaternion.LookRotation(rawDir);
oldConPos = con.localPosition;
}
Vector3 oldConPos;
Vector3 rawDir = Vector3.forward;
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;
}
}
[Serializable]
public class Lerper
{
public float t = 0;
public float spring = 1;
public float dampen = 1;
float vel;
public void Update(float to = 1, bool bounce = false)
{
float dir = to - t;
vel += dir * spring * Time.deltaTime;
if (Mathf.Sign(vel) != Mathf.Sign(dir))
{
vel *= 1 - (dampen * Time.deltaTime);
}
else
{
vel *= 1 - (dampen * 0.33f * Time.deltaTime);
}
float newt = t + vel * Time.deltaTime;
if (bounce && (newt < 0 || newt > 1))
{
vel *= -0.5f;
newt = Mathf.Clamp01(newt);
}
t = newt;
}
public void Reset()
{
t = vel = 0;
}
}