|
@@ -0,0 +1,107 @@
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+
|
|
|
+namespace SC.XR.Unity
|
|
|
+{
|
|
|
+ public class CameraFollowerLangChao : FollowerBase
|
|
|
+ {
|
|
|
+ [Header("Following Settings")]
|
|
|
+ public bool InstantFollowing;
|
|
|
+ public bool LinearFollowing;
|
|
|
+ public Vector2 menu_size;
|
|
|
+
|
|
|
+ private Transform _slamHead;
|
|
|
+ Vector2 initViewPoint;
|
|
|
+ Vector3 viewPoint = Vector3.zero;
|
|
|
+ Vector3 viewPoint_right = Vector3.zero;
|
|
|
+ Vector3 viewPoint_left = Vector3.zero;
|
|
|
+ Vector3 viewPoint_top = Vector3.zero;
|
|
|
+ Vector3 viewPoint_bot = Vector3.zero;
|
|
|
+ bool isFollower = false;
|
|
|
+ Vector3 originPos = Vector3.zero;
|
|
|
+ Vector3 desPos = Vector3.zero;
|
|
|
+ float journeyLength = 0f;
|
|
|
+ void Start()
|
|
|
+ {
|
|
|
+ _slamHead = Camera.main.transform;
|
|
|
+ initViewPoint = Camera.main.WorldToViewportPoint(_slamHead.position + (_slamHead.forward * WindowDistance));
|
|
|
+ InstantFollow();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected override void Follow()
|
|
|
+ {
|
|
|
+ if (Camera.main == null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Vector3 v3 = CalculateWindowPosition(Camera.main.transform);
|
|
|
+ transform.position = Vector3.Lerp(transform.position,new Vector3(v3.x, Camera.main.transform.position.y, v3.z) , WindowFollowSpeed * Time.deltaTime);
|
|
|
+ transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler( new Vector3(0, CalculateWindowRotation(Camera.main.transform).eulerAngles.y, 0)), WindowFollowSpeed * Time.deltaTime);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected void InstantFollow()
|
|
|
+ {
|
|
|
+ Vector3 v3 = CalculateWindowPosition(Camera.main.transform);
|
|
|
+ transform.position = new Vector3(v3.x, Camera.main.transform.position.y, v3.z);
|
|
|
+ transform.eulerAngles =new Vector3(0, CalculateWindowRotation(Camera.main.transform).eulerAngles.y,0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected void LinearFollow()
|
|
|
+ {
|
|
|
+ originPos = transform.position;
|
|
|
+ desPos = CalculateWindowPosition(Camera.main.transform);
|
|
|
+ journeyLength = Vector3.Distance(originPos, desPos);
|
|
|
+ transform.position = Vector3.Lerp(originPos, new Vector3(desPos.x, Camera.main.transform.position.y, desPos.z), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
|
|
|
+ transform.position = new Vector3(transform.position.x, Camera.main.transform.position.y, transform.position.z);
|
|
|
+ transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(new Vector3(0, CalculateWindowRotation(Camera.main.transform).eulerAngles.y, 0)), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void LateUpdate()
|
|
|
+ {
|
|
|
+ if ((StopFollower == false)&& !InstantFollowing)
|
|
|
+ {
|
|
|
+ if (IsFollower() && !LinearFollowing)
|
|
|
+ {
|
|
|
+ Follow();
|
|
|
+ }
|
|
|
+ else if (IsFollower() && LinearFollowing)
|
|
|
+ {
|
|
|
+ LinearFollow();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if ((StopFollower == false) && InstantFollowing)
|
|
|
+ {
|
|
|
+ InstantFollow();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected bool IsFollower()
|
|
|
+ {
|
|
|
+ if (Camera.main == null || Camera.main == null)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ viewPoint = Camera.main.WorldToViewportPoint(transform.position);
|
|
|
+ viewPoint_right = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(menu_size.x/2, 0, 0)));
|
|
|
+ viewPoint_left = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(-menu_size.x/2, 0, 0)));
|
|
|
+ viewPoint_top = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, menu_size.y/2, 0)));
|
|
|
+ viewPoint_bot = Camera.main.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, -menu_size.y/2, 0)));
|
|
|
+
|
|
|
+ if (viewPoint_right.x > 1 || viewPoint_left.x < 0 || viewPoint_bot.y < 0 || viewPoint_top.y > 1)
|
|
|
+ {
|
|
|
+ isFollower = true;
|
|
|
+ }
|
|
|
+ else if (Mathf.Abs(viewPoint.x - initViewPoint.x) < 0.05f && Mathf.Abs(viewPoint.y - initViewPoint.y) < 0.05f)
|
|
|
+ {
|
|
|
+ isFollower = false;
|
|
|
+ }
|
|
|
+ return isFollower;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|