CapersProject/Assets/02.Scripts/Ui/Title/TycoonTable.cs
2024-11-19 21:53:00 +09:00

57 lines
1.5 KiB
C#

using PixelCrushers.DialogueSystem.ChatMapper;
using UnityEngine;
public class TycoonTable : MonoBehaviour
{
private RectTransform selfRectTransform;
public RectTransform parentRectTransform; // 부모 UI 객체의 RectTransform
public float smoothSpeed = 5f;
private Vector3 targetPosition;
void Start()
{
selfRectTransform = GetComponent<RectTransform>();
if (selfRectTransform == null)
{
Debug.LogError("이 스크립트는 RectTransform이 필요한 UI 객체에서만 작동합니다.");
}
targetPosition = selfRectTransform.anchoredPosition;
}
void Update()
{
UpdateTargetPosition();
SmoothMoveToTarget();
}
void UpdateTargetPosition()
{
if (selfRectTransform == null) return;
Vector3 mousePosition = Input.mousePosition;
float parentWidth = parentRectTransform.rect.width;
float normalizedX = (mousePosition.x / Screen.width) * 2f - 1f;
// X축과 Y축 목표 위치 계산
float localX = normalizedX * (parentWidth / 2f);
// 목표 위치 업데이트
targetPosition = new Vector3(localX / 100.0f, 0f, 0f);
}
void SmoothMoveToTarget()
{
// 부드럽게 목표 위치로 이동
selfRectTransform.anchoredPosition = Vector3.Lerp(
selfRectTransform.anchoredPosition, // 현재 위치
targetPosition, // 목표 위치
Time.deltaTime * smoothSpeed // 부드럽게 이동 속도
);
}
}