유튜브 강의를 보면서 디테일만 빼고 거의 똑같이 만들었다.
게임 만드는 법 학습 후 다음 프로젝트로 넘어가서 내 게임을 만들자.
box collider 2d is trigger 체크 시
- 충돌 감지 ㅇ, 물리적충돌반응 X,
- private void OnTriggerEnter2D(Collider2D other) {}
- 콜라이더엔터에도 적용 (콜라이더엔터는 물리충돌only일 때 적용)
edit
preferences
external Tools
external Script Editor
Browse
vscode설치폴더 code.exe
opengameart.org로 들어가서 검색하거나
spaceship sprite site:opengameart.org 로 검색
virus site:opengameart.org
bullet site:opengameart.org
space background site:opengameart.org
spinning coin site:opengameart.org
캐릭터
적
미사일
배경
코인
유니티프로젝트
Assets / Sprites 에 드래그
edit
project settings
editor
enter play mode options에 체크
//private는 다른 class에서 변수값을 변경할 수 없게 하는
private float moveSpeed = 3;
https://dotnet.microsoft.com/en-us/download
여기서 NET Core SDK 설치 (시스템 환경변수추가확인)
재시작
vscode
ctrl + shift + p
프로젝트클릭
유니티로 vscode진입하면 해결
23.7.23
"Horizontal"
"Vertical"
여기서 H V 가 원래는 h v 소문자여서 에러가 남 > 대문자로 변경
유니티에서 edit > project settings > input manager > axis 가면
정확한 명칭을 알 수 있다.
충돌 반경 적용
재미를 위해 기체보다 훨씬 작게 만들었다
게임화면 밖으로 플레이어가 나가지 못하게 Wall 적용
마우스컨트롤을 하면 Wall가 무용지물이 된다.
그래서 커서를 화면안에 가둠
미사일 이미지가 배경투명이 아니라 연사가 빠르면 가린다 수정 필요함
missile.cs에서
1초 후 오브젝트가 사라지게
Vector3.down으로 아래로 내려가게
디스트로이로 화면 밖 벗어나면 사라지게 하고
박스콜라이더2D로 충돌 반경 만들기
박스콜라이더2D (충돌반경 적용)
리지드바디2D 중력0 (물리법칙 적용)
프리팹으로 에너미 1~7 보스1 만들기
각 박스콜라이더2D로 충돌반경 조절
보스는 서클콜라이더2D 적용
에너미 생성 위치를 선언하기 위한
void Start()
{
// 0 1 2 3 4 5 6 에서 랜덤뽑기
//int index = Random.Range(0,7);
// enemies.Length에서 enemis배열크기 즉 7로 들어감
int index = Random.Range(0,enemies.Length);
SpawnEnemy(arrPosX[0],index);
}
void SpawnEnemy(float posX, int index){
//에너미 스폰 위치를 선언
Vector3 spawnPos = new Vector3(posX, transform.position.y, transform.position.z);
Instantiate(enemies[index],spawnPos,Quaternion.identity);
}
foreach로 5개의 포지션에서 모두 생성
23.7.24
플레이어 애셋 바탕제거하고 서클콜라이더2D 더 작게
미사일도 바탕 제거로 깔끔
적 스폰 인터벌
일정 스폰마다 센 적으로 변경
IEnumerator EnemyRoutine(){
//3초대기 후 while true문
yield return new WaitForSeconds(1f);
int spawnCount=0;
int enemyIndex=0;
while(true){
foreach (float posX in arrPosX){
//int index = Random.Range(0,enemies.Length);
SpawnEnemy(posX,enemyIndex);
}
spawnCount+=1;
if(spawnCount%2==0){
enemyIndex+=1;
}
yield return new WaitForSeconds(spawnInterval);
}
void SpawnEnemy(float posX, int index){
//에너미 스폰 위치를 선언
Vector3 spawnPos = new Vector3(posX, transform.position.y, transform.position.z);
// 01234중 랜덤으로 뽑고 0이면 즉 20% 확률로 에너미 인덱스1을 올려서 센몹을 20확률로 스폰
if (Random.Range(0,5)==0){
index+=1;
}
//에너미 인덱스 최대치넘어갔을 때 생기는 에러 디버깅
if (index >= enemies.Length){
index = enemies.Length -1 ;
}
Instantiate(enemies[index],spawnPos,Quaternion.identity);
}
3:32 적 시간 지날수록 더 빠르게 내려오기
halo + trail renderer
23.7.24
23.7.30
//플레이어 충돌 처리
private void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.tag=="enemy"){
Debug.Log("게임 오버");
Destroy(gameObject);
}
}
}
1 서클콜라이더2D 충돌 반경은 코인 크기 보다 크게
2 애니메이션 적용
3 리지드바디2D로 중력 적용, 그레비티스케일은 약하게
4 코인 랜덤 점프
void Jump(){
Rigidbody2D rigidBody = GetComponent<Rigidbody2D>();
float randomJumpForce = Random.Range(0.1f, 1.5f);
//벡터2는 엑스 와이 값만 가진다
Vector2 jumpVelocity = Vector2.up * randomJumpForce;
jumpVelocity.x=Random.Range(-0.5f,0.5f);
rigidBody.AddForce(jumpVelocity, ForceMode2D.Impulse);
}
private void OnTriggerEnter2D(Collider2D other) {
// 충돌한 오브젝트가 적인지 확인
if (other.gameObject.CompareTag("missile"))
{
//Debug.Log('ss');
Missile missile = other.gameObject.GetComponent<Missile>();
hp-=missile.damage;
if (hp <=0){
Destroy(gameObject); //에너미 삭제
//에너미 삭제하면서 코인 만들기
Instantiate(coin, transform.position, Quaternion.identity);
}
// 미사일 삭제
Destroy(other.gameObject);
}
에너미 프리팹 7개 동시 선택 후 코인 프리팹을 에너미 프리팹 스크립트 coin부분에 드래그 다운
//플레이어 충돌 처리
private void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.tag=="enemy"){
Debug.Log("게임 오버");
Destroy(gameObject); //적이랑 충돌 > 플레이어 디스트로이
} else if (other.gameObject.tag=="coin"){
Debug.Log("코인 +1");
Destroy(other.gameObject); //코인이랑 충돌 > 코인 디스트로이
}
}
}
우클릭 > UI > 패널
좌측하단정렬
public void IncreaseCoin(){
coin += 1;
text.SetText(coin.ToString());
}
셋텍스트(coin) > 인트 to 스트링 에러 > 투스트링메소드로 스트링으로
boss 태그 추가
에너미 ctrl c ctrl v
스프라이츠 보스로 바꾸고 boss태그로
에너미스포너 스크립트 : void SpawnBoss(){}, 에너미 인덱스 끝에서 SpawnBoss함수 실행 후 에너미 인덱스 0으로
public void SetGameOver(){
EnemySpawner enemySpawner = FindObjectOfType<EnemySpawner>();
if (enemySpawner != null){
enemySpawner.StopEnemyRoutine();
}
}
private void OnTriggerEnter2D(Collider2D other) {
// 충돌한 오브젝트가 적인지 확인
if (other.gameObject.CompareTag("missile"))
{
//Debug.Log('ss');
Missile missile = other.gameObject.GetComponent<Missile>();
hp-=missile.damage;
if (hp <=0){
if(gameObject.tag=="boss"){
GameManager.instance.SetGameOver();
}
Destroy(gameObject); //에너미 삭제
//에너미 삭제하면서 코인 만들기
Instantiate(coin, transform.position, Quaternion.identity);
Instantiate(coin, transform.position, Quaternion.identity);
Instantiate(coin, transform.position, Quaternion.identity);
Instantiate(coin, transform.position, Quaternion.identity);
}
// 미사일 삭제
Destroy(other.gameObject);
}
게임오버 패널추가
재시작구현
23.8.2
- if (enemyIndex < enemies.Length){ 로 보스 생성 했을 땐 enemy스폰을 막고
- bossCounter변수를 추가하여 보스가 1번 생성되면 보스를 추가스폰하는 것을 막아서
- enemy 페이즈 > boss 페이즈 > 게임오버(리스타트)
로 스테이지1을 완성한다.IEnumerator EnemyRoutine(){ //1초대기 후 while true문 yield return new WaitForSeconds(1f); float moveSpeed=0.1f; int spawnCount=0; int enemyIndex=0; while(true){ if (enemyIndex < enemies.Length){ foreach (float posX in arrPosX){ //int index = Random.Range(0,enemies.Length); SpawnEnemy(posX,enemyIndex, moveSpeed); } spawnCount+=1; if(spawnCount%2==0){ enemyIndex+=1; //enemyIndex=Random.Range(0,enemies.Length); //2스폰넘어갈때마다 에너미 무브스피드 증가 moveSpeed+=0.01f; } } else if (enemyIndex >= enemies.Length && bossCounter==0){ SpawnBoss(); // enemyIndex=0; // moveSpeed=5f; bossCounter=1; } yield return new WaitForSeconds(spawnInterval); } }
슈팅게임인데 적이 미사일을 안쏘는 이상한게임
피하기 게임 + 슈팅게임의 느낌
밸런스 패치 등은 생략하고 게임 만다는 법을 공부했으니
다음 프로젝트로 넘어가서 내 게임을 만든다
좋은 정보 감사합니다