NG_/Assets/Scripts/UI/全局/UIMove.cs
2024-12-13 19:40:05 +08:00

41 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UIMove : MonoBehaviour,IPointerClickHandler
{
public Transform targetObject; // 目标对象
public Vector3 relativePosition; // 相对位置
private bool isMoving = false;
public static bool isStaticMove = false;
void Update()
{
if (isMoving)
{
// 使用平滑过渡Lerp将目标对象移动到相对位置
if (targetObject != null)
{
targetObject.localPosition = Vector3.Lerp(targetObject.localPosition, relativePosition, Time.deltaTime*2f);
if (Vector3.Distance(targetObject.localPosition, relativePosition) < 1f)
{
isMoving = false;
isStaticMove = false;
}
}
}
}
public void OnPointerClick(PointerEventData eventData)
{
if (isStaticMove)
{
return;
}
isMoving = true;
isStaticMove = true;
}
}