[랜덤]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Test3
{
//씬의 모든 객체들을 관리
public class Test_CreatePortalMain : MonoBehaviour
{
[SerializeField]
private MonsterGenerator monsterGenerator;
[SerializeField]
private GameObject portal;
private List<MonsterController> monsterList;
// Start is called before the first frame update
void Start()
{
//컬렉션 사용전 반드시 초기화
this.monsterList = new List<MonsterController>();
MonsterController turtle = this.monsterGenerator.Generate(GameEnums.eMonsterType.Turtle, new Vector3(-3, 0, 0));
MonsterController slime = this.monsterGenerator.Generate(GameEnums.eMonsterType.Slime, new Vector3(0, 0, 3));
//만들어진 개체들을 그룹화 관리
//배열, 컬렉션
//동적 배열
this.monsterList.Add(turtle);
this.monsterList.Add(slime);
Debug.LogFormat("this.monsterList.Count: {0}", this.monsterList.Count);
//리스트의 요소를 출력
foreach (MonsterController monster in this.monsterList)
{
Debug.LogFormat("monster: {0}", monster);
}
}
// Update is called once per frame
void Update()
{
//Test
//Ray연습할겸 클릭해서 선택 몬스터를 제거하자
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 2f);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100f))
{
if(hit.collider.tag == "Monster")
{
int i = 0;
Debug.LogFormat("hit.collider.tag: {0}", hit.collider.tag);
Destroy(hit.collider.gameObject);
this.monsterList.Remove(monsterList[i++]);
Debug.LogFormat("남은 몬스터의 수: {0}", monsterList.Count);
if(monsterList.Count == 0)
{
float randX = Random.Range(-3f, 3f);
float randZ = Random.Range(-3f, 3f);
this.portal.transform.position = new Vector3(randX, 0f, randZ);
this.portal.SetActive(true);
}
}
}
}
}
}
}
'유니티 기초' 카테고리의 다른 글
무기, 방패 장착, 제거 - 생성 시 부모를 지정, 생성 후 부모를 지정 (0) | 2023.08.11 |
---|---|
몬스터 2초 후 삭제, 아이템 드랍, 아이템 씬에서 제거 후 아이템 로그 출력 (0) | 2023.08.10 |
몬스터 동적으로 데이터관리 생성 (0) | 2023.08.10 |
Real SimpleRPG (0) | 2023.08.09 |
SimpleRPG Test (0) | 2023.08.09 |