SCKeyboardFollower.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using DG.Tweening;
  2. using System.Collections;
  3. using UnityEngine;
  4. namespace SC.XR.Unity {
  5. [RequireComponent(typeof(SCKeyboardMono))]
  6. public class SCKeyboardFollower : FollowerBase {
  7. Vector3 viewPoint = Vector3.zero;
  8. bool isFollowing = false;
  9. Vector2 initViewPoint;
  10. protected override void OnEnable() {
  11. base.OnEnable();
  12. initViewPoint = SvrManager.Instance.leftCamera.WorldToViewportPoint(CalculateWindowPosition(SvrManager.Instance.head));
  13. isFollowing = true;
  14. }
  15. protected override void Follow() {
  16. if (SvrManager.Instance.leftCamera == null || SvrManager.Instance.head == null)
  17. return;
  18. viewPoint = SvrManager.Instance.leftCamera.WorldToViewportPoint(transform.position);
  19. if (viewPoint.x < (initViewPoint.x - 1f) || viewPoint.y < (initViewPoint.y - 0.5f) || viewPoint.x > (initViewPoint.x + 1f) || viewPoint.y > (initViewPoint.y + 1f) || Vector3.Magnitude(SvrManager.Instance.head.position - transform.position) > (WindowDistance+0.1f)) {
  20. isFollowing = true;
  21. } else if (Mathf.Abs(viewPoint.x - initViewPoint.x) < 0.03f && Mathf.Abs(viewPoint.y - initViewPoint.y) < 0.03f) {
  22. isFollowing = false;
  23. }
  24. if (isFollowing) {
  25. transform.position = Vector3.Lerp(transform.position, CalculateWindowPosition(SvrManager.Instance.head), WindowFollowSpeed * Time.deltaTime);
  26. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(SvrManager.Instance.head), WindowFollowSpeed * Time.deltaTime);
  27. }
  28. }
  29. }
  30. }