Okami

VRRig

Sep 16th, 2020
967
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using UnityEngine;
  3.  
  4. [Serializable]
  5. public class VRMap
  6. {
  7.     public Transform vrTarget;
  8.     public Transform rigTarget;
  9.     public Vector3 trackingPositionOffset;
  10.     public Vector3 trackingRotationOffset;
  11.  
  12.     public void Map()
  13.     {
  14.         rigTarget.position = vrTarget.TransformDirection(trackingPositionOffset);
  15.         rigTarget.rotation = vrTarget.rotation * Quaternion.Euler(trackingRotationOffset);
  16.     }
  17. }
  18.  
  19. public class VRRig : MonoBehaviour
  20. {
  21.     public VRMap head = new VRMap();
  22.     public VRMap leftHand = new VRMap();
  23.     public VRMap rightHand = new VRMap();
  24.  
  25.     public Transform headConstraint;
  26.     private Vector3 headBodyOffset;
  27.  
  28.     // Start is called before the first frame update
  29.     void Start()
  30.     {
  31.         headBodyOffset = transform.position - headConstraint.position;
  32.     }
  33.  
  34.     // Update is called once per frame
  35.     void Update()
  36.     {
  37.         transform.position = headConstraint.position + headBodyOffset;
  38.         transform.forward = Vector3.ProjectOnPlane(headConstraint.up, Vector3.up).normalized;
  39.  
  40.  
  41.         head.Map();
  42.         leftHand.Map();
  43.         rightHand.Map();
  44.     }
  45. }
  46.  
RAW Paste Data