From c2f792186a48f802eb17e6fc5bc38f6edffb04f1 Mon Sep 17 00:00:00 2001 From: bottlefish <781230111@qq.com> Date: Thu, 16 Jan 2025 18:24:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=88=E6=81=A2=E5=A4=8D=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E8=A7=86=E8=A7=89=E7=A7=BB=E5=8A=A8=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Resources/Yarn/PeipeiTalk1/身体检查.yarn | 2 +- Assets/Scenes/PeipeiFixScene.unity | 3 + .../FixSystem/Effect/MouseFollowEffect.cs | 87 +++++++++++++------ 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/Assets/Resources/Yarn/PeipeiTalk1/身体检查.yarn b/Assets/Resources/Yarn/PeipeiTalk1/身体检查.yarn index fcd686291..c0f0ab861 100644 --- a/Assets/Resources/Yarn/PeipeiTalk1/身体检查.yarn +++ b/Assets/Resources/Yarn/PeipeiTalk1/身体检查.yarn @@ -467,7 +467,7 @@ colorID: 3 group:视觉检查 position: -2052,-2351 --- -<> +<> me:没什么头绪,先看看别的地方 <> <> diff --git a/Assets/Scenes/PeipeiFixScene.unity b/Assets/Scenes/PeipeiFixScene.unity index 0dfd18342..d4e9ed0f6 100644 --- a/Assets/Scenes/PeipeiFixScene.unity +++ b/Assets/Scenes/PeipeiFixScene.unity @@ -3887,6 +3887,9 @@ MonoBehaviour: zoomDuration: 2 sceneBoundsMin: {x: 37.6, y: -6.64} sceneBoundsMax: {x: 42.2, y: -5.21} + viewCamera: {fileID: 105412842} + parallaxEffectMultiplier: 0.1 + maxOffset: {x: 2, y: 2} --- !u!1001 &188784455 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/FixSystem/Effect/MouseFollowEffect.cs b/Assets/Scripts/FixSystem/Effect/MouseFollowEffect.cs index eb98c0eaa..c886843bc 100644 --- a/Assets/Scripts/FixSystem/Effect/MouseFollowEffect.cs +++ b/Assets/Scripts/FixSystem/Effect/MouseFollowEffect.cs @@ -19,6 +19,14 @@ public class MouseFollowAndZoom : MonoBehaviour private bool isActive = false; private bool isLocked = false; + public Camera viewCamera; + public float parallaxEffectMultiplier = 0.1f; + public Vector2 maxOffset = new Vector2(2f, 2f); + + private Vector3 initialPosition; + + private float viewOffset = 40.3f; + // 读写访问 public void SetIsActive(bool value) { @@ -33,6 +41,7 @@ public class MouseFollowAndZoom : MonoBehaviour void Start() { + initialPosition = transform.position; targetIndicator.SetActive(false); // 初始时隐藏目标框 eyemanager = FindObjectOfType(); if (eyemanager == null) @@ -59,6 +68,33 @@ 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; } @@ -66,7 +102,8 @@ public class MouseFollowAndZoom : MonoBehaviour { // 获取鼠标位置 Vector3 mousePosition = Input.mousePosition; - Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePosition); + mousePosition.x += viewOffset; // 添加偏移量 + Vector3 worldPosition = viewCamera.ScreenToWorldPoint(mousePosition); RaycastHit2D hit = Physics2D.Raycast(worldPosition, Vector2.zero); // 检查是否有目标 @@ -96,35 +133,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(); - // if (boxCollider != null) - // { - // Vector3 boxCenter = boxCollider.bounds.center; - // Vector3 boxSize = boxCollider.bounds.size; + // 获取目标的 BoxCollider2D + BoxCollider2D boxCollider = currentTarget.GetComponent(); + 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(); - // indicatorRectTransform.position = screenCenter; - // indicatorRectTransform.sizeDelta = screenSize; + // 设置目标框位置和大小 + RectTransform indicatorRectTransform = targetIndicator.GetComponent(); + 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)