한 걸음 두 걸음

unity 유니티 ] 네비게이션 / 목표물로 향하게 하기 본문

Unity

unity 유니티 ] 네비게이션 / 목표물로 향하게 하기

언제나 변함없이 2019. 5. 26. 11:12
반응형

팩맨과 유령에서 유령이 팩맨(target)한테 이동하도록 하는 코드입니다.

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

public class navi : MonoBehaviour
{
    /* 목표물(pacman)으로 이동하는 네비게이션 스크립트입니다.*/

    public Transform target;
    Vector3 destination;
    NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        destination = agent.destination;

    }

    void Update()
    {
        if (Vector3.Distance(destination, target.position) > 1.0f)
        {
            destination = target.position;
            agent.destination = destination;
        }

    }
}
반응형