using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlatformAccountFollower : FollowerBase { [Header("Following Settings")] public bool InstantFollowing = false;//是否无延迟跟随 public bool LinearFollowing;//是否线性移动 public Vector2 border_x; //x轴边界 public Vector2 border_y; //y轴边界 private Transform _slamHead; Vector2 initViewPoint;//相机画面的中点,坐标为(0.5f,0.5f) Vector3 viewPoint = Vector3.zero;// 面板中心坐标相对于相机画面的坐标。 private Vector3 velocity = Vector3.zero; bool isFollower = false; Vector3 originPos = Vector3.zero; Vector3 desPos = Vector3.zero; float journeyLength = 0f; protected override void OnEnable() { base.OnEnable(); } private void Start() { InitTrans(); } protected override void LateUpdate() { if (StopFollower == false) { if (IsFollower()) { if (LinearFollowing) { LinearFollow(); } else { Follow(); } } } } void InitTrans() { if (Camera.main != null) { _slamHead = Camera.main.transform; transform.position = CalculateWindowPosition(Camera.main.transform); transform.rotation = CalculateWindowRotation(Camera.main.transform); initViewPoint = Camera.main.WorldToViewportPoint(_slamHead.position + (_slamHead.forward * WindowDistance));//相机画面中点坐标 } } protected bool IsFollower() { if ( Camera.main == null) { return false; } viewPoint = Camera.main.WorldToViewportPoint(transform.position);// 面板在相机画面上的坐标 if (viewPoint.x > initViewPoint.x + border_x.x + CalculateWindowOffsetX() || viewPoint.x < initViewPoint.x + border_x.y + CalculateWindowOffsetX() || viewPoint.y < initViewPoint.y + border_y.y + CalculateWindowOffsetY() || viewPoint.y > initViewPoint.y + border_y.x + CalculateWindowOffsetY())//边界判断 { isFollower = true; } else if (Mathf.Abs(viewPoint.x - initViewPoint.x - CalculateWindowOffsetX()) < 0.03f && Mathf.Abs(viewPoint.y - initViewPoint.y - CalculateWindowOffsetY()) < 0.06f) { isFollower = false; } return isFollower; } //Nonlinear Following by default protected override void Follow() { transform.position = Vector3.Lerp(transform.position, CalculateWindowPosition(Camera.main.transform), WindowFollowSpeed * Time.deltaTime); //transform.position = Vector3.SmoothDamp(transform.position, CalculateWindowPosition(Camera.main.transform), ref velocity, WindowFollowSpeed); transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), WindowFollowSpeed * Time.deltaTime); } //For Linear Following, turn down the WindowFollowSpeed to around 0.5 at distance = 1 for best experience protected void LinearFollow() { originPos = transform.position; desPos = CalculateWindowPosition(Camera.main.transform); journeyLength = Vector3.Distance(originPos, desPos); transform.position = Vector3.Lerp(originPos, desPos, (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed); transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed); } protected override Vector3 CalculateWindowPosition(Transform cameraTransform) { Vector3 position = cameraTransform.position + (Vector3.ProjectOnPlane(cameraTransform.forward,Vector3.up).normalized * WindowDistance); Vector3 horizontalOffset = cameraTransform.right * windowOffset.x; Vector3 verticalOffset = cameraTransform.up * windowOffset.y; switch (windowAnchor) { case TextAnchor.UpperLeft: position += verticalOffset - horizontalOffset; break; case TextAnchor.UpperCenter: position += verticalOffset; break; case TextAnchor.UpperRight: position += verticalOffset + horizontalOffset; break; case TextAnchor.MiddleLeft: position -= horizontalOffset; break; case TextAnchor.MiddleRight: position += horizontalOffset; break; case TextAnchor.MiddleCenter: position += horizontalOffset + verticalOffset; break; case TextAnchor.LowerLeft: position -= verticalOffset + horizontalOffset; break; case TextAnchor.LowerCenter: position -= verticalOffset; break; case TextAnchor.LowerRight: position -= verticalOffset - horizontalOffset; break; } return position; } protected override Quaternion CalculateWindowRotation(Transform cameraTransform) { Quaternion rotation = Quaternion.Euler(0, cameraTransform.rotation.eulerAngles.y, 0); switch (windowAnchor) { case TextAnchor.UpperLeft: rotation *= windowHorizontalRotationInverse * windowVerticalRotationInverse; break; case TextAnchor.UpperCenter: rotation *= windowHorizontalRotationInverse; break; case TextAnchor.UpperRight: rotation *= windowHorizontalRotationInverse * windowVerticalRotation; break; case TextAnchor.MiddleLeft: rotation *= windowVerticalRotationInverse; break; case TextAnchor.MiddleRight: rotation *= windowVerticalRotation; break; case TextAnchor.LowerLeft: rotation *= windowHorizontalRotation * windowVerticalRotationInverse; break; case TextAnchor.LowerCenter: rotation *= windowHorizontalRotation; break; case TextAnchor.LowerRight: rotation *= windowHorizontalRotation * windowVerticalRotation; break; } return rotation; } }