154 lines
No EOL
4.4 KiB
C#
154 lines
No EOL
4.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.XR;
|
|
|
|
public class Rig : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
public Design design;
|
|
public Main main;
|
|
public GameObject disconnected;
|
|
public Transform head, con, 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);
|
|
}
|
|
|
|
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
|
|
head.localPosition = InputTracking.GetLocalPosition(XRNode.Head);
|
|
head.localRotation = InputTracking.GetLocalRotation(XRNode.Head);
|
|
|
|
con.localPosition = InputTracking.GetLocalPosition(XRNode.RightHand);
|
|
con.localRotation = InputTracking.GetLocalRotation(XRNode.RightHand);
|
|
|
|
|
|
// °inview
|
|
transform.localScale = Vector3.one * design.scale;
|
|
transform.position = -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);
|
|
}
|
|
|
|
// Input Rotation
|
|
// 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);
|
|
// }
|
|
|
|
// Ignoring current implementation
|
|
if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, ovrCon))
|
|
{
|
|
pullFrom = con.localPosition;
|
|
}
|
|
|
|
if (OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger, ovrCon))
|
|
{
|
|
pull = con.localPosition - pullFrom;
|
|
if (pull.magnitude > 0.025f / 3)
|
|
{
|
|
rawDir = pull.normalized;
|
|
}
|
|
|
|
// DRAG PIVOT
|
|
pull = Vector3.ClampMagnitude(pull, 0.025f);
|
|
pullFrom = con.localPosition - pull;
|
|
}
|
|
|
|
inputRot = Quaternion.LookRotation(rawDir);
|
|
|
|
// visualize
|
|
pullLine.SetPosition(0, transform.TransformPoint(pullFrom));
|
|
pullLine.SetPosition(1, transform.TransformPoint(pullFrom + pull));
|
|
}
|
|
Vector3 rawDir = Vector3.forward;
|
|
Vector3 pullFrom, pull;
|
|
public LineRenderer pullLine;
|
|
|
|
|
|
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;
|
|
}
|
|
} |