77 lines
2.3 KiB
C#
77 lines
2.3 KiB
C#
using Spine.Unity;
|
|
using UnityEngine;
|
|
|
|
public class TycoonCharacter : MonoBehaviour
|
|
{
|
|
private RectTransform selfRectTransform;
|
|
public RectTransform parentRectTransform; // 부모 UI 객체의 RectTransform
|
|
|
|
public float smoothSpeed = 5f;
|
|
private Vector3 targetPosition;
|
|
|
|
|
|
[field: SerializeField]
|
|
private SkeletonAnimation spine;
|
|
|
|
[field: SerializeField]
|
|
private SkeletonRootMotion spineEye;
|
|
|
|
void Start()
|
|
{
|
|
selfRectTransform = GetComponent<RectTransform>();
|
|
|
|
if (selfRectTransform == null)
|
|
{
|
|
Debug.LogError("이 스크립트는 RectTransform이 필요한 UI 객체에서만 작동합니다.");
|
|
}
|
|
|
|
targetPosition = selfRectTransform.anchoredPosition;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
UpdateTargetPosition();
|
|
SmoothMoveToTarget();
|
|
|
|
if (spineEye != null)
|
|
{
|
|
// 본의 위치 변경 (예: 마우스 위치로 이동)
|
|
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
spineEye.RootMotionBone.WorldX = mousePosition.x;
|
|
spineEye.RootMotionBone.WorldY = mousePosition.y;
|
|
spineEye.RootMotionBone.UpdateWorldTransform();
|
|
// 스켈레톤을 다시 업데이트
|
|
}
|
|
}
|
|
|
|
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 // 부드럽게 이동 속도
|
|
);
|
|
}
|
|
} |