💾 코드
코드내용
📖 참고
참고할 코드 내용
💾 코드
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using UnityEngine;
using UnityEngine.Serialization;
public class MovementController : MonoBehaviour
{
public float speed;
public GameObject destinationObj;
public bool isMove = false;
public Coroutine curCoroutine;
private void Update()
{
if(speed <= 0 || !destinationObj)
return;
if (!isMove)
{
curCoroutine = StartCoroutine(Movement());
isMove = true;
}
}
private IEnumerator Movement()
{
while (Vector2.Distance(destinationObj.transform.position, gameObject.transform.root.position) > 0.1f)
{
Vector2 moveDirection = (destinationObj.transform.position - gameObject.transform.root.position).normalized;
Vector2 moveAmount = moveDirection * speed * Time.deltaTime;
transform.root.position += new Vector3(moveAmount.x, moveAmount.y, 0f);
yield return null;
}
StopCoroutine(curCoroutine);
isMove = false;
destinationObj = null;
}
}