일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 수학
- 1월
- 유니티
- 2022년
- 유니티 심화과정
- 10월
- 백준
- c++
- 자료 구조
- 단계별로 풀어보기
- 다이나믹 프로그래밍
- C/C++
- todolist
- 개인 프로젝트
- 기초
- 코딩 테스트
- 골드메탈
- 7월
- 2월
- 2023년
- 3월
- 코딩 기초 트레이닝
- 5월
- 게임 엔진 공부
- 개인 프로젝트 - 런앤건
- 입문
- 2024년
- 프로그래머스
- 4월
- 2025년
- Today
- Total
기록 보관소
[Unity/유니티] 기초-2D 종스크롤 슈팅: 적 비행기 만들기[B29] 본문
개요
유니티 입문과 독학을 위해서 아래 링크의 골드메탈님의 영상들을 보며 진행 상황 사진 또는 캡처를 올리고 배웠던 점을 요약해서 적는다.
현재는 영상들을 보고 따라하고 배우는 것에 집중할 것이며, 영상을 모두 보고 따라한 후에는 개인 프로젝트를 설계하고 직접 만드는 것이 목표다.
https://youtube.com/playlist?list=PLO-mt5Iu5TeYI4dbYwWP8JqZMC9iuUIW2
유니티 강좌 기초 채널 Basic
유니티 개발을 처음 시작하시는 입문자 분들을 위한 기초 채널. [ 프로젝트 ] B00 ~ B12 (BE1) : 유니티 필수 기초 B13 ~ B19 (BE2) : 2D 플랫포머 B20 ~ B26 (BE3) : 2D 탑다운 대화형 RPG B27 ~ B37 (BE4) : 2D 종스크롤
www.youtube.com
2D 종스크롤 슈팅: 적 비행기 만들기[B29]
1. 준비하기
2. 적 기체 프리펩
//Bullet 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
public int dmg;
void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.tag == "BorderBullet") {
Destroy(gameObject);
}
}
}
//Enemy 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public float speed;
public int health;
public Sprite[] sprites;
SpriteRenderer spriteRenderer;
Rigidbody2D rigid;
void Awake() {
spriteRenderer = GetComponent<SpriteRenderer>();
rigid = GetComponent<Rigidbody2D>();
rigid.velocity = Vector2.down * speed;
}
void onHit(int dmg) {
health -= dmg;
spriteRenderer.sprite = sprites[1]; //평소 스프라이트 0, 피격시 스프라이트 1
Invoke("ReturnSprite", 0.1f);
if (health <= 0)
Destroy(gameObject);
}
void ReturnSprite() {
spriteRenderer.sprite = sprites[0];
}
void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.tag == "BorderBullet") //맵의 경계로 가게되면
Destroy(gameObject);
else if (collision.gameObject.tag == "PlayerBullet") { //플레이어 총알에 닿으면
Bullet bullet = collision.gameObject.GetComponent<Bullet>();
onHit(bullet.dmg);
Destroy(collision.gameObject); //플레이어 총알 삭제
}
}
}
3. 적 기체 생성
//GameManager 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
public GameObject[] enemyObjs;
public Transform[] spawnPoints;
public float maxSpawnDelay;
public float curSpawnDelay;
void Update() {
curSpawnDelay += Time.deltaTime;
if (curSpawnDelay > maxSpawnDelay) {
SpawnEnemy();
maxSpawnDelay = Random.Range(0.5f, 3f);
curSpawnDelay = 0;
}
}
void SpawnEnemy() {
int ranEnemy = Random.Range(0, 3);
int ranPoint = Random.Range(0, 5);
Instantiate(enemyObjs[ranEnemy], spawnPoints[ranPoint].position, spawnPoints[ranPoint].rotation);
}
}
- Random.Range(float, float / int, int); 정해진 범위 내의 숫자를 랜덤으로 반환하는 함수. 매개변수 중 뒷 숫자는 범위에서 제외되며(ex. 매개변수가 0, 3이면 0, 1, 2만 반환된다), 매개변수 타입에따라 반환 타입이 결정된다. 참고로 비슷한 함수인 Random.RandomRange()도 있지만, 이 함수는 오래된 사용하지 않는 함수라고 한다.
'유니티 프로젝트 > 2D 종스크롤 슈팅' 카테고리의 다른 글
[Unity/유니티] 기초-2D 종스크롤 슈팅: 아이템과 필살기 구현하기[B32] (0) | 2022.03.04 |
---|---|
[Unity/유니티] 기초-2D 종스크롤 슈팅: UI 간단하게 완성하기[B31] (0) | 2022.03.03 |
[Unity/유니티] 기초-2D 종스크롤 슈팅: 적 전투와 피격 이벤트 만들기[B30] (0) | 2022.03.02 |
[Unity/유니티] 기초-2D 종스크롤 슈팅: 총알발사 구현하기[B28] (0) | 2022.02.27 |
[Unity/유니티] 기초-2D 종스크롤 슈팅: 플레이어 이동 구현하기[B27] (0) | 2022.02.26 |