상세 컨텐츠

본문 제목

유니티 그리드시스템에 타워 설치 및 적 바라보기(추적)

유니티/기능

by MJ_119 2024. 9. 4. 15:18

본문

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

public class WayPoint : MonoBehaviour
{
    [SerializeField] GameObject TowerPrefab;
    [SerializeField] bool isPlaceable = true;
    void OnMouseDown()
    {
        if (isPlaceable)
        {
            Instantiate(TowerPrefab, transform.position, Quaternion.identity);
            isPlaceable = false;
        }
    }
}

 

바닥 타일에 박스콜라이더랑 스크립트에 isplaceable이 체크(true)되어있어야 디펜스타워를 설치할 수 있음.

중복설치는 못하게끔 한번 프리팹 소환하고나서 isplaceable = false로 바꿈

 

 

 

@ 방어타워가 적 추적, 바라보기

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

public class EnemyTargeting : MonoBehaviour
{
    // 바라볼 타워오브젝트(프리팹) 설정
    [SerializeField] GameObject traceTower;

    Transform target;
    void Start()
    {
        // 적의 위치 찾기
        target = FindObjectOfType<EnemyMover>().transform;
    }

    void Update()
    {
        TraceEnemy();
    }

    private void TraceEnemy()
    {
        // 적 바라보기
        transform.LookAt(target);
    }
}

 

방어 타워 프리팹에 맨위에다가 스크립트를 추가하고 적을 바라볼 타워의 상단부(BallistaTopMesh)를 넣어주고 실행한다.

관련글 더보기