Skip to content

Instantly share code, notes, and snippets.

@PatHightree
Created November 23, 2023 22:36
Show Gist options
  • Save PatHightree/06ad1112581c7303f2c9ed42665b6309 to your computer and use it in GitHub Desktop.
Save PatHightree/06ad1112581c7303f2c9ed42665b6309 to your computer and use it in GitHub Desktop.
Script to attach an object to the HMD and update the pos/rot just before rendering
using UnityEngine;
using UnityEngine.InputSystem;
public class HeadAttach : MonoBehaviour
{
public Transform Target;
public InputAction HmdPosition;
public InputAction HmdRotation;
private void Awake()
{
HmdPosition.Enable();
HmdRotation.Enable();
}
void OnEnable()
{
Application.onBeforeRender += ApplicationOnBeforeRender;
}
private void OnDisable()
{
Application.onBeforeRender -= ApplicationOnBeforeRender;
}
private void ApplicationOnBeforeRender()
{
Target.position = HmdPosition.ReadValue<Vector3>();
Target.rotation = HmdRotation.ReadValue<Quaternion>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment