CameraFollower.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 && enabled) {
  27. //yield return new WaitForSeconds(time);
  28. if (SvrManager.Instance && SvrManager.Instance.IsRunning) {
  29. //viewPoint = SvrManager.Instance.leftCamera.WorldToViewportPoint(transform.position);
  30. worldPoint = SvrManager.Instance.leftCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, followDistance));
  31. // if (viewPoint.x < 0.4 || viewPoint.y < 0.4 || viewPoint.x >0.6 || viewPoint.y >0.6) {
  32. //Debug.Log("Start Following");
  33. //Vector3 angle = SvrManager.Instance.leftCamera.transform.eulerAngles;
  34. ////transform.DORotate(new Vector3(0, angle.y, 0), 0.5f).SetEase(Ease.InQuart).SetAutoKill(true);
  35. //transform.DORotate(angle, 0.2f).SetEase(Ease.InQuart).SetAutoKill(true);
  36. //transform.DOMove(worldPoint, 0.5f).SetEase(Ease.OutCubic).SetAutoKill(true);
  37. transform.position = Vector3.Lerp(transform.position, worldPoint, 0.05f);
  38. transform.rotation = Quaternion.Lerp(transform.rotation, SvrManager.Instance.leftCamera.transform.rotation,0.05f);
  39. // }
  40. }
  41. yield return null;
  42. }
  43. }
  44. }