SCKeyboardFollower.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 = Camera.main.WorldToViewportPoint(CalculateWindowPosition(Camera.main.transform));
  13. isFollowing = true;
  14. StopFollower = false;
  15. }
  16. protected override void Follow() {
  17. if (Camera.main == null || Camera.main.transform == null)
  18. return;
  19. viewPoint = Camera.main.WorldToViewportPoint(transform.position);
  20. if (viewPoint.x < (initViewPoint.x - 1f) || viewPoint.y < (initViewPoint.y - 0.5f) || viewPoint.x > (initViewPoint.x + 1f) || viewPoint.y > (initViewPoint.y + 1f) || Vector3.Magnitude(Camera.main.transform.position - transform.position) > (WindowDistance+0.1f)) {
  21. isFollowing = true;
  22. } else if (Mathf.Abs(viewPoint.x - initViewPoint.x) < 0.03f && Mathf.Abs(viewPoint.y - initViewPoint.y) < 0.03f) {
  23. isFollowing = false;
  24. StopFollower = true;
  25. }
  26. if (isFollowing) {
  27. transform.position = Vector3.Lerp(transform.position, CalculateWindowPosition(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  28. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  29. }
  30. }
  31. }
  32. }