Unity: VR Head Blocking (Oculus Integration)
VRHeadBlocking.cs:
using System.Collections; using System.Collections.Generic; using UnityEngine; // VR Head Blocking Script (Synaptic Response) // For use with Unity + Oculus Integration (v23.1) // Add this script to the CenterEyeAnchor component of the OVRCameraRig // If your player component has colliders set its label to "Player" // Initial Implementation: Scott Lupton (02.17.2021)
public class VRHeadBlocking : MonoBehaviour { public GameObject player; private int layerMask; private Collider[] objs = new Collider[10]; private Vector3 prevHeadPos; private float backupCap = .2f; private void Start() { layerMask = 1 << 8; layerMask = ~layerMask; prevHeadPos = transform.position; } private int DetectHit(Vector3 loc) { int hits = 0; int size = Physics.OverlapSphereNonAlloc(loc, backupCap, objs, layerMask, QueryTriggerInteraction.Ignore); for (int i = 0 ; i < size; i++) { if (objs[i].tag != "Player") { hits++; } } return hits; } public void Update() { if (player != null) { int hits = DetectHit(transform.position); // No collision if (hits == 0) prevHeadPos = transform.position; // Collision else { Vector3 headDiff = transform.position - prevHeadPos; Vector3 adjHeadPos = new Vector3(player.transform.position.x-headDiff.x, player.transform.position.y, player.transform.position.z-headDiff.z); player.transform.SetPositionAndRotation(adjHeadPos, player.transform.rotation); } } } }
Comments
Post a Comment