CapersProject/Assets/02.Scripts/Ui/Title/TycoonCharacter.cs

82 lines
2.4 KiB
C#
Raw Normal View History

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