LookAtUserGrabInteractable.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // /******************************************************************************
  2. // * File: LookAtUserGrabInteractable.cs
  3. // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
  4. // *
  5. // * Confidential and Proprietary - Qualcomm Technologies, Inc.
  6. // *
  7. // ******************************************************************************/
  8. using QCHT.Samples.XRKeyboard;
  9. using UnityEngine;
  10. using UnityEngine.XR.Interaction.Toolkit;
  11. namespace QCHT.Samples
  12. {
  13. public class LookAtUserGrabInteractable : XRGrabInteractable
  14. {
  15. private Transform _userTransform;
  16. private bool _shouldFaceUser = false;
  17. private float _xStartingAngle;
  18. private void Start()
  19. {
  20. _userTransform = Camera.main.transform;
  21. selectEntered.AddListener(arg => _shouldFaceUser = true);
  22. selectExited.AddListener(arg => _shouldFaceUser = false);
  23. _xStartingAngle = transform.eulerAngles.x;
  24. var pointerViewerKeyboard = FindObjectOfType<PointerViewerKeyboard>();
  25. if (pointerViewerKeyboard != null) pointerViewerKeyboard.KeyboardTransform = transform;
  26. }
  27. private void Update()
  28. {
  29. if (!_shouldFaceUser)
  30. return;
  31. Quaternion quaternion = Quaternion.LookRotation(transform.position - _userTransform.position);
  32. transform.rotation = quaternion;
  33. transform.eulerAngles = new Vector3(_xStartingAngle, transform.eulerAngles.y, transform.eulerAngles.z);
  34. }
  35. }
  36. }