Giant vs Human 팀프로젝트 개발일지

[Giant vs Human 팀프로젝트, Mobile] 대포가 주변에 있으면 대포발사 버튼이 활성화되고 버튼을 누르면 포탄이 발사

다모아 2023. 11. 29. 20:44

 

대포 근처에 도달하게되면 공격버튼이 활성화된다.

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

public class TestTowerAround : MonoBehaviour
{
    [SerializeField] private Transform towerTr;
    [SerializeField] private Button btnTowerAttack;
    [SerializeField] private Transform bulletTr;
    [SerializeField] private GameObject bulletGo;

    private void Start()
    {
        this.btnTowerAttack.onClick.AddListener(() => {
            GameObject go = Instantiate(this.bulletGo);
            go.transform.position = this.bulletTr.position;
            go.transform.rotation = this.bulletTr.rotation;
        });
    }

    // Update is called once per frame
    void Update()
    {
        float dis = Vector3.Distance(this.transform.position, this.towerTr.position);
        Debug.Log(dis);

        if(dis <= 0.4f)
        {
            this.btnTowerAttack.gameObject.SetActive(true);
        }
        else
        {
            this.btnTowerAttack.gameObject.SetActive(false);
        }
    }
}