본문 바로가기
유니티강좌

지정한 위치(경로) 따라서 오브젝트 이동 시키기 | Waypoint Movement, Move Path | 유니티 2D게임 개발(Unity & C#) 튜토리얼

by Ncube 2020. 12. 26.

 

 

배열을 이용하여 강아지가 먹이 그릇들이 놓인 위치로 이동하는 방법을 알아봅니다. 초기 위치로 이동하면 다시 반복. Sprite Editor로 오브젝트의 중심축 조절에 대해서도 배워봅시다.

 

#지정위치오브젝트이동  #WaypointMovement  #MoveTowards

 

 

 

[WayPointMove.cs]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WayPointMove : MonoBehaviour
{
    [SerializeField] Transform[] foodPos;
    [SerializeField] float speed = 5f;
    int foodNum = 0;

    // Start is called before the first frame update
    void Start()
    {
        transform.position = foodPos[foodNum].transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        MovePath();
    }

    public void MovePath()
    {
        transform.position = Vector2.MoveTowards
            (transform.position, foodPos[foodNum].transform.position, speed * Time.deltaTime);

        if (transform.position == foodPos[foodNum].transform.position)
            foodNum++;

        if (foodNum == foodPos.Length)
            foodNum = 0;
    }
}

 

댓글