SCKeyboardFollower.cs 1.7 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 = API_GSXR_Slam.SlamManager.leftCamera.WorldToViewportPoint(CalculateWindowPosition(API_GSXR_Slam.SlamManager.head));
  13. isFollowing = true;
  14. }
  15. protected override void Follow() {
  16. if (API_GSXR_Slam.SlamManager.leftCamera == null || API_GSXR_Slam.SlamManager.head == null)
  17. return;
  18. viewPoint = API_GSXR_Slam.SlamManager.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(API_GSXR_Slam.SlamManager.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(API_GSXR_Slam.SlamManager.head), WindowFollowSpeed * Time.deltaTime);
  26. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(API_GSXR_Slam.SlamManager.head), WindowFollowSpeed * Time.deltaTime);
  27. }
  28. }
  29. }
  30. }