2024/02/01

안석환·2024년 2월 1일

🧐기본 공부 내용


  1. 공부내용 핵심 키워드
  • 설명


💾 코드

코드내용

📖 참고

참고할 코드 내용




⛔️ 에러


  1. NPC 리팩토링 중 오류
  • 시도: 코루틴이 끝나는 시점을 While 문이 끝났을 때로 고쳤다 : While 문이 끝나기전에 목적지 거리가 갱신되어 While 문이 끝나질 않았다.
  • 시도: 코루틴이 끝나는 시점을 While문 안쪽으로 넣었다 : 어째서인지 안됐다
  • 시도: 코루틴의 시작조건에 Bool값을 주고 있었는데 그 불값을 돌려 놓는 시점을 While문이 끝나는 시점으로 변경해주었다
  • 해결: 다음 코루틴이 정상적으로 시작되었다
  • 알게된점: 코루틴이 끝나는 시점에 코루틴이 시작하는 조건을 초기화 해주니 원할하게 진행되는것 같다, InitSetting 타이밍에 해당 객체의 필요 변수를 초기화해주 진행해주는게 원할한 진행을 도와주는 것 같다.
    모든 시작점엔 진행했던 모든 것들을 초기화 해주는 습관이 필요해 보인다.

💾 코드

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;
    }
}




💭 느낀점


  1. 힘들다 쉬자
  • 쉰다 오늘은
profile
안석환!

0개의 댓글