저 화살표 스폰자리에 적 소환 하고 싶은데
아무리 수정 해도 안되네
using System.Collections;
using UnityEngine;
public class Spawner : MonoBehaviour {
public Wave[] waves;
public Enemy enemy;
Wave currentWave;
int currentWaveNumber;
public bool isSpawn = false;
public float spawnDelay = 1.5f;
float spawnTimer = 0f;
int enemiesRemainingToSpawn;
int enemiesRemainingAlive;
float nextSpawnTime;
void Start() {
NextWave();
}
Vector3[] positions = new Vector3[5];
void CreatePositions()
{
float viewPosY = 1.2f;
float viewPosX = 0f;
float gapX = 1f / 6f;
for (int i = 0; i {
viewPosX = gapX + gapX * i;
Vector3 viewPos = new Vector3(viewPosX, viewPosY, 0);
Vector3 worldPos = Camera.main.ViewportToWorldPoint(viewPos);
worldPos.z = 0f;
positions[i] = worldPos;
}
}
void Update() {
float randomX = Random.Range(-0.5f, 0.5f);
if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime) {
enemiesRemainingToSpawn--;
nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;
Enemy spawnedEnemy = Instantiate(enemy, new Vector3(randomX, 1.1f, 0f), Quaternion.identity) as Enemy;
spawnedEnemy.OnDeath += OnEnemyDeath;
}
}
void OnEnemyDeath() {
enemiesRemainingAlive--;
if (enemiesRemainingAlive == 0) {
NextWave();
}
}
void NextWave() {
currentWaveNumber++;
if (currentWaveNumber - 1 currentWave = waves [currentWaveNumber - 1];
enemiesRemainingToSpawn = currentWave.enemyCount;
enemiesRemainingAlive = enemiesRemainingToSpawn;
}
}
[System.Serializable]
public class Wave {
public int enemyCount;
public float timeBetweenSpawns;
}
}
코드를 정확히 몰라서 확신은 못내리겠는데 Enemy타입이 스크립트아니야??
Enemy spawnedEnemy = Instantiate(enemy, new Vector3(randomX, 1.1f, 0f), Quaternion.identity) as Enemy; 이부분이 생산부 같은데
Instantiate로 Enemy 스크립트를 생산하는게 아니라 GameObject를 생산해야 할 것 같은데
public GameObject enemy;로 선언하고 Prefab으로 만든 게임오브젝트 넣어주고 코드 동일하게 가면 될듯?
처음에 Enemy 를 잘못 선언했네. 윗 갤러가 알려준게 맞는듯함. GameObject 로 선언을 해줘야 하는데 스크립트를 선언해놓고, Update 에서 Enemy 를 다시 선언하면서 그걸 게임오브젝트처럼 불러오려 하고 있잖아.
아래글 지금봣는데 소환자체는 되는거구나 그러면 위에 글은 다 쓸모없는 소리네
아래글에서 소환은 되는게 확실하면 Enemy spawnedEnemy = Instantiate(enemy, new Vector3(@여기값), Quaternion.identity) as Enemy; @여기값이라고 쓴 곳을 수정해보면서 실행하면 소환위치가 바뀌는게 보임?
소환위치가 바뀐다면 vector3값에 원하는 좌표 넣으면 될꺼고 소환위치가 안바뀐다면 다른게 문제인거지
내가 이해를 못한건가 화살표 표시된 부분 게임 오브젝트 벡터값을 Instantiate 인자값 두번째에 넣어
변수 선언부에 public Transform spawnPoint; 쓰고 인스펙터에 생긴 칸에 화살표부분 오브젝트 집어넣어 그다음 instantiate 두번째 인자값에 spawnPoint.position 써봐 이걸 원하는게 맞는진 모르겠지만