CameraFollower.cs 1.7 KB

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