
using System.Collections;
using UnityEngine;
public class MovingObject : MonoBehaviour
{
[SerializeField] RectTransform _targetUI = null;
float speed = 0.2f;
void Start()
{
MoveToUI(_targetUI);
}
public void MoveToUI(RectTransform targetUI)
{
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(targetUI, targetUI.position, Camera.main, out Vector3 targetPos))
{
StartCoroutine(Move(targetPos));
}
}
IEnumerator Move(Vector3 pos)
{
Vector3 dir = pos - transform.position; ;
while (Vector3.Distance(transform.position, pos) > 0.1f)
{
transform.position += dir * speed * Time.deltaTime;
yield return null;
}
Debug.Log("Arrive!");
}
}
'Unity' 카테고리의 다른 글
[Unity] 자주쓰는 유니티 오픈소스 에셋(Git) (0) | 2023.02.02 |
---|---|
[Unity] LineRenderer 를 활용해 화살표 그리기! (0) | 2023.02.02 |
[Unity] 반지름 R인 원 위에 일정 간격(각도)의 위치 얻기. (0) | 2023.02.02 |
[Unity] 숫자 단위(K,M,B) 로 바꾸기 (0) | 2023.01.30 |