重构视觉的移动
This commit is contained in:
@@ -3,40 +3,22 @@ using UnityEngine.UI;
|
||||
using DG.Tweening;
|
||||
using TMPro;
|
||||
using AibisDream;
|
||||
using Cinemachine;
|
||||
|
||||
public class MouseFollowAndZoom : MonoBehaviour
|
||||
{
|
||||
public float parallaxEffectMultiplier = 0.1f;
|
||||
public Vector2 maxOffset = new Vector2(2f, 2f);
|
||||
public CinemachineVirtualCamera virtualCamera;
|
||||
public GameObject targetIndicator; // UI 目标框
|
||||
public TMP_Text targetNameText; // 显示目标名称的文本
|
||||
public Button exitZoomButton; // 退出缩放的按钮
|
||||
public float zoomInScale = 1.5f; // 缩放倍数
|
||||
public float zoomDuration = 1.0f; // 缩放持续时间(减慢)
|
||||
public Vector2 sceneBoundsMin; // 场景边界的左下角
|
||||
public Vector2 sceneBoundsMax; // 场景边界的右上角
|
||||
|
||||
public Camera viewCamera;
|
||||
|
||||
//private Camera MainCamera;
|
||||
private Vector3 initialPosition;
|
||||
private Vector3 zoomedInPosition; // 保存缩放时的相机位置
|
||||
private bool isZoomedIn = false;
|
||||
private EyeSystem eyemanager;
|
||||
private EyeTarget currentTarget; // 当前目标
|
||||
|
||||
private float viewOffset = 40.3f;
|
||||
|
||||
private bool isActive = false;
|
||||
|
||||
private bool isLocked = false;
|
||||
|
||||
// 只读访问
|
||||
public bool GetMyPrivateVariable()
|
||||
{
|
||||
return isActive;
|
||||
}
|
||||
|
||||
// 读写访问
|
||||
public void SetIsActive(bool value)
|
||||
{
|
||||
@@ -51,7 +33,6 @@ public class MouseFollowAndZoom : MonoBehaviour
|
||||
|
||||
void Start()
|
||||
{
|
||||
initialPosition = transform.position;
|
||||
targetIndicator.SetActive(false); // 初始时隐藏目标框
|
||||
eyemanager = FindObjectOfType<EyeSystem>();
|
||||
if (eyemanager == null)
|
||||
@@ -78,41 +59,14 @@ public class MouseFollowAndZoom : MonoBehaviour
|
||||
|
||||
void HandleMouseMovement()
|
||||
{
|
||||
// 获取鼠标位置
|
||||
Vector3 mousePosition = Input.mousePosition;
|
||||
Vector3 worldMousePosition = viewCamera.ScreenToWorldPoint(mousePosition);
|
||||
worldMousePosition.z = 0; // 确保 z 为 0
|
||||
|
||||
// 计算目标偏移量并反向
|
||||
Vector3 targetOffset = (viewCamera.transform.position - worldMousePosition) * parallaxEffectMultiplier;
|
||||
targetOffset.x = Mathf.Clamp(targetOffset.x, -maxOffset.x, maxOffset.x);
|
||||
targetOffset.y = Mathf.Clamp(targetOffset.y, -maxOffset.y, maxOffset.y);
|
||||
|
||||
// 平滑处理偏移量
|
||||
float smoothFactor = 0.01f; // 平滑系数,值越小平滑效果越强,响应越慢
|
||||
currentOffset = Vector3.Lerp(currentOffset, targetOffset, smoothFactor);
|
||||
|
||||
// 计算目标位置
|
||||
Vector3 targetPosition = initialPosition + currentOffset;
|
||||
|
||||
// 限制目标位置在场景边界范围内
|
||||
targetPosition.x = Mathf.Clamp(targetPosition.x, sceneBoundsMin.x, sceneBoundsMax.x);
|
||||
targetPosition.y = Mathf.Clamp(targetPosition.y, sceneBoundsMin.y, sceneBoundsMax.y);
|
||||
|
||||
// 使用插值(Lerp)平滑更新位置
|
||||
float followSpeed = 0.1f; // 跟随速度,值越小延迟越大
|
||||
Vector3 smoothedPosition = Vector3.Lerp(transform.position, targetPosition, followSpeed);
|
||||
|
||||
// 应用平滑后的位置
|
||||
transform.position = smoothedPosition;
|
||||
|
||||
}
|
||||
|
||||
void UpdateCurrentTarget()
|
||||
{
|
||||
// 获取鼠标位置
|
||||
Vector3 mousePosition = Input.mousePosition;
|
||||
mousePosition.x += viewOffset; // 添加偏移量
|
||||
Vector3 worldPosition = viewCamera.ScreenToWorldPoint(mousePosition);
|
||||
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePosition);
|
||||
RaycastHit2D hit = Physics2D.Raycast(worldPosition, Vector2.zero);
|
||||
|
||||
// 检查是否有目标
|
||||
@@ -142,35 +96,35 @@ public class MouseFollowAndZoom : MonoBehaviour
|
||||
|
||||
void HandleTargetIndicator()
|
||||
{
|
||||
if (currentTarget == null)
|
||||
{
|
||||
targetIndicator.SetActive(false); // 隐藏指示器
|
||||
return;
|
||||
}
|
||||
// if (currentTarget == null)
|
||||
// {
|
||||
// targetIndicator.SetActive(false); // 隐藏指示器
|
||||
// return;
|
||||
// }
|
||||
|
||||
// 获取目标的 BoxCollider2D
|
||||
BoxCollider2D boxCollider = currentTarget.GetComponent<BoxCollider2D>();
|
||||
if (boxCollider != null)
|
||||
{
|
||||
Vector3 boxCenter = boxCollider.bounds.center;
|
||||
Vector3 boxSize = boxCollider.bounds.size;
|
||||
// // 获取目标的 BoxCollider2D
|
||||
// BoxCollider2D boxCollider = currentTarget.GetComponent<BoxCollider2D>();
|
||||
// if (boxCollider != null)
|
||||
// {
|
||||
// Vector3 boxCenter = boxCollider.bounds.center;
|
||||
// Vector3 boxSize = boxCollider.bounds.size;
|
||||
|
||||
// 转换为屏幕坐标
|
||||
Vector3 screenCenter = viewCamera.WorldToScreenPoint(boxCenter);
|
||||
Vector2 screenSize = viewCamera.WorldToScreenPoint(boxCenter + boxSize) -
|
||||
viewCamera.WorldToScreenPoint(boxCenter);
|
||||
// // 转换为屏幕坐标
|
||||
// Vector3 screenCenter = viewCamera.WorldToScreenPoint(boxCenter);
|
||||
// Vector2 screenSize = viewCamera.WorldToScreenPoint(boxCenter + boxSize) -
|
||||
// viewCamera.WorldToScreenPoint(boxCenter);
|
||||
|
||||
// 设置目标框位置和大小
|
||||
RectTransform indicatorRectTransform = targetIndicator.GetComponent<RectTransform>();
|
||||
indicatorRectTransform.position = screenCenter;
|
||||
indicatorRectTransform.sizeDelta = screenSize;
|
||||
// // 设置目标框位置和大小
|
||||
// RectTransform indicatorRectTransform = targetIndicator.GetComponent<RectTransform>();
|
||||
// indicatorRectTransform.position = screenCenter;
|
||||
// indicatorRectTransform.sizeDelta = screenSize;
|
||||
|
||||
// 设置目标名称
|
||||
targetNameText.text = currentTarget.name;
|
||||
// // 设置目标名称
|
||||
// targetNameText.text = currentTarget.name;
|
||||
|
||||
// 显示指示器
|
||||
targetIndicator.SetActive(true);
|
||||
}
|
||||
// // 显示指示器
|
||||
// targetIndicator.SetActive(true);
|
||||
// }
|
||||
}
|
||||
|
||||
public void SetIndicatorTarget(EyeTarget target)
|
||||
@@ -178,78 +132,6 @@ public class MouseFollowAndZoom : MonoBehaviour
|
||||
currentTarget = target;
|
||||
}
|
||||
|
||||
|
||||
// void HandleTargetIndicator()
|
||||
// {
|
||||
// // 获取鼠标位置
|
||||
// Vector3 mousePosition = Input.mousePosition;
|
||||
// mousePosition.x+=viewOffset;
|
||||
// Vector3 worldPosition = viewCamera.ScreenToWorldPoint(mousePosition);
|
||||
// //worldPosition.x+=viewOffset;
|
||||
// RaycastHit2D hit = Physics2D.Raycast(worldPosition, Vector2.zero);
|
||||
|
||||
// if(hit.collider!=null)
|
||||
// {
|
||||
// Debug.Log(hit.collider.gameObject.name);
|
||||
// }
|
||||
|
||||
|
||||
// if (hit.collider != null && hit.collider.CompareTag("EyeTarget")) // 检查是否是目标 Sprite
|
||||
// {
|
||||
// Debug.Log("find target");
|
||||
// targetIndicator.SetActive(true);
|
||||
// BoxCollider2D boxCollider = hit.collider.GetComponent<BoxCollider2D>();
|
||||
|
||||
// if (boxCollider != null)
|
||||
// {
|
||||
// // 获取 BoxCollider2D 的大小和位置
|
||||
// Vector3 boxCenter = boxCollider.bounds.center;
|
||||
// //boxCenter.x-=viewOffset;
|
||||
// Vector3 boxSize = boxCollider.bounds.size;
|
||||
|
||||
// //Vector3 boxCenterModify=new Vector3(boxCenter.x-viewOffset,boxCenter.y,boxCenter.z);
|
||||
// Vector3 boxCenterModify=boxCenter;
|
||||
|
||||
// // 转换为屏幕坐标
|
||||
// Vector3 screenCenter = viewCamera.WorldToScreenPoint(boxCenterModify);
|
||||
// Vector2 screenSize = viewCamera.WorldToScreenPoint(boxCenterModify + boxSize) - viewCamera.WorldToScreenPoint(boxCenterModify);
|
||||
|
||||
// // 设置目标框位置和大小
|
||||
// RectTransform indicatorRectTransform = targetIndicator.GetComponent<RectTransform>();
|
||||
// indicatorRectTransform.position = screenCenter;
|
||||
|
||||
// // 动画效果:先放大再缩小
|
||||
// //indicatorRectTransform.localScale = Vector3.one * 1.5f;
|
||||
// //indicatorRectTransform.DOScale(Vector3.one, 0.2f).SetEase(Ease.OutBounce);
|
||||
// indicatorRectTransform.sizeDelta = screenSize;
|
||||
|
||||
// // 设置目标名称
|
||||
// targetNameText.text = hit.collider.name;
|
||||
// }
|
||||
|
||||
// //点击目标框触发缩放
|
||||
// if (Input.GetMouseButtonDown(0))
|
||||
// {
|
||||
// Debug.Log("点击目标触发了");
|
||||
// EyeTarget target;
|
||||
|
||||
// if (hit.collider != null && hit.collider.GetComponent<EyeTarget>() != null)
|
||||
// {
|
||||
// isInTargetProcessing = true;
|
||||
// target = hit.collider.GetComponent<EyeTarget>();
|
||||
// eyemanager.SetTarget(target);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Debug.LogWarning("The collider does not have an EyeTarget component.");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// targetIndicator.SetActive(false);
|
||||
// }
|
||||
// }
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
// 设置 Gizmos 颜色
|
||||
|
||||
Reference in New Issue
Block a user