using UnityEngine; public class TycoonCharacter : MonoBehaviour { private RectTransform selfRectTransform; public RectTransform parentRectTransform; // 부모 UI 객체의 RectTransform public float smoothSpeed = 5f; private Vector3 targetPosition; void Start() { selfRectTransform = GetComponent(); 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 parentHeight = parentRectTransform.rect.height; float normalizedX = (mousePosition.x / Screen.width) * 2f - 1f; float normalizedY = (mousePosition.y / Screen.height) * 2f - 1f; // X축과 Y축 목표 위치 계산 float localX = normalizedX * (parentWidth / 2f); float localY = normalizedY * (parentHeight / 2f); // 목표 위치 업데이트 targetPosition = new Vector3(- localX / 30.0f + 450, - localY / 30.0f, 0f); } void SmoothMoveToTarget() { // 부드럽게 목표 위치로 이동 selfRectTransform.anchoredPosition = Vector3.Lerp( selfRectTransform.anchoredPosition, // 현재 위치 targetPosition, // 목표 위치 Time.deltaTime * smoothSpeed // 부드럽게 이동 속도 ); } }