유니티

[Unity] Target 지정해서 이동하기

simstealer 2022. 9. 21. 10:30
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 따라갈 타겟 설정
private Transform target;

// 접근 거리
[Header("접근 거리")]
public float cantactDistance = 1.0f;
    
void Start()
    {
        target = GameObject.FindGameObjectWithTag("Player").transform;
    }

private void FollowTarget()
    {// 내 위치와 타겟 위치의 거리를 구하고 접근 거리보다 크면 따라 가게 된다.
        if (Vector3.Distance(transform.position, target.transform.position) > cantactDistance)
        {// Vector3.MoveTowards() - 일정한 속도로 타겟까지 이동하는 함수 입니다.
            transform.position = Vector3.MoveTowards(transform.position, target.transform.position, moveSpeed * Time.deltaTime);
        }
    }