PositionFollower.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using DG.Tweening;
  2. using SC;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class PositionFollower : MonoBehaviour {
  7. Vector3 worldPoint = Vector3.zero;
  8. [SerializeField]
  9. private float delDistance = 1;
  10. [SerializeField]
  11. private float followTime = 2;
  12. //[SerializeField]
  13. //private float followAngle = 60;
  14. private Vector3 lastestPosition;
  15. Coroutine followCoroutine;
  16. // Use this for initialization
  17. void OnEnable() {
  18. followCoroutine = StartCoroutine(Following(followTime));
  19. lastestPosition = transform.position;
  20. }
  21. void Disable() {
  22. if (followCoroutine != null) {
  23. StopCoroutine(followCoroutine);
  24. }
  25. }
  26. IEnumerator Following(float time) {
  27. while (true) {
  28. yield return new WaitForSeconds(time);
  29. yield return null;
  30. if (SvrManager.Instance && SvrManager.Instance.IsRunning) {
  31. worldPoint = SvrManager.Instance.leftCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
  32. if (Vector3.Distance(lastestPosition, SvrManager.Instance.leftCamera.transform.position) > delDistance) {
  33. Debug.Log("Start PositionFollower");
  34. lastestPosition = transform.position;
  35. //Vector3 angle = SvrManager.Instance.leftCamera.transform.eulerAngles;
  36. //transform.DORotate(new Vector3(0, angle.y, 0), 0.5f).SetEase(Ease.InQuart).SetAutoKill(true);
  37. //transform.DORotate(angle, 0.2f).SetEase(Ease.InQuart).SetAutoKill(true);
  38. transform.DOMove(worldPoint, 0.5f).SetEase(Ease.OutCubic).SetAutoKill(true);
  39. }
  40. }
  41. }
  42. }
  43. }