일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 유니티
- 유니티 심화과정
- 2022년
- 3월
- 7월
- 개인 프로젝트
- 5월
- 입문
- 다이나믹 프로그래밍
- 2025년
- c++
- 4월
- 2023년
- 단계별로 풀어보기
- 기초
- 프로그래머스
- 수학
- 2월
- 자료 구조
- 10월
- 골드메탈
- 2024년
- 개인 프로젝트 - 런앤건
- 게임 엔진 공부
- 코딩 기초 트레이닝
- todolist
- 1월
- 코딩 테스트
- C/C++
- 백준
- Today
- Total
기록 보관소
[Unity/유니티] 기초-2D 종스크롤 슈팅: 총알발사 구현하기[B28] 본문
개요
유니티 입문과 독학을 위해서 아래 링크의 골드메탈님의 영상들을 보며 진행 상황 사진 또는 캡처를 올리고 배웠던 점을 요약해서 적는다.
현재는 영상들을 보고 따라하고 배우는 것에 집중할 것이며, 영상을 모두 보고 따라한 후에는 개인 프로젝트를 설계하고 직접 만드는 것이 목표다.
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 종스크롤 슈팅: 총알발사 구현하기[B28]
1. 준비하기
- 이번 슈팅 게임에서 플레이어가 발사하는 총알에 중력 설정은 필요없으므로 Rigidbody의 Gravity Scale은 0으로 설정해준다.
- 프리펩 : 재활용을 위해 에셋으로 저장된 게임 오브젝트
- 참고로 오브젝트 복사할때 단축키는 Ctrl + D다. Ctrl + C에 Ctrl + V도 가능하지만 Ctrl + D는 복사 붙여넣기 없이 바로 가능하다.
2. 발사체 제거
//Bullet 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.tag == "BorderBullet") {
Destroy(gameObject);
}
}
}
- Destroy(오브젝트) : 매개변수 오브젝트를 삭제하는 함수. MonoBehaviour에 들어있는 기본 함수다.
3. 발사체 생성
//Player 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public GameObject bulletObjA;
public GameObject bulletObjB;
Animator anim;
public float speed;
public bool isTouchTop;
public bool isTouchBottom;
public bool isTouchLeft;
public bool isTouchRight;
void Awake() {
anim = GetComponent<Animator>();
}
void Update() {
Move();
Fire();
}
void Move() { //플레이어 이동 함수
float h = Input.GetAxisRaw("Horizontal");
if ((isTouchRight && h == 1) || (isTouchLeft && h == -1))
h = 0;
float v = Input.GetAxisRaw("Vertical");
if ((isTouchTop && v == 1) || (isTouchBottom && v == -1))
v = 0;
Vector3 curPos = transform.position;
Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;
transform.position = curPos + nextPos;
if (Input.GetButtonDown("Horizontal") || Input.GetButtonUp("Horizontal"))
anim.SetInteger("Input", (int)h);
}
void Fire() { //플레이어 총알 발사 함수
GameObject bullet = Instantiate(bulletObjA, transform.position, transform.rotation);
Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
rigid.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
}
void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.tag == "Border") {
switch(collision.gameObject.name) {
case "Top":
isTouchTop = true;
break;
case "Bottom":
isTouchBottom = true;
break;
case "Left":
isTouchLeft = true;
break;
case "Right":
isTouchRight = true;
break;
}
}
}
void OnTriggerExit2D(Collider2D collision) {
if (collision.gameObject.tag == "Border") {
switch (collision.gameObject.name) {
case "Top":
isTouchTop = false;
break;
case "Bottom":
isTouchBottom = false;
break;
case "Left":
isTouchLeft = false;
break;
case "Right":
isTouchRight = false;
break;
}
}
}
}
- Instantiate(오브젝트, 위치, 방향) : 매개변수 오브젝트를 생성하는 함수
4. 발사체 다듬기
//Player 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public GameObject bulletObjA;
public GameObject bulletObjB;
Animator anim;
public float speed;
public float maxShotDelay;
public float curShotDelay;
public bool isTouchTop;
public bool isTouchBottom;
public bool isTouchLeft;
public bool isTouchRight;
void Awake() {
anim = GetComponent<Animator>();
}
void Update() {
Move();
Fire();
Reload();
}
void Move() { //플레이어 이동 함수
float h = Input.GetAxisRaw("Horizontal");
if ((isTouchRight && h == 1) || (isTouchLeft && h == -1))
h = 0;
float v = Input.GetAxisRaw("Vertical");
if ((isTouchTop && v == 1) || (isTouchBottom && v == -1))
v = 0;
Vector3 curPos = transform.position;
Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;
transform.position = curPos + nextPos;
if (Input.GetButtonDown("Horizontal") || Input.GetButtonUp("Horizontal"))
anim.SetInteger("Input", (int)h);
}
void Fire() { //플레이어 총알 발사 함수
if (!Input.GetButton("Fire1")) //Ctrl 키
return;
if (curShotDelay < maxShotDelay) //장전 중이라면
return;
GameObject bullet = Instantiate(bulletObjA, transform.position, transform.rotation);
Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
rigid.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
curShotDelay = 0;
}
void Reload() {
curShotDelay += Time.deltaTime;
}
void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.tag == "Border") {
switch(collision.gameObject.name) {
case "Top":
isTouchTop = true;
break;
case "Bottom":
isTouchBottom = true;
break;
case "Left":
isTouchLeft = true;
break;
case "Right":
isTouchRight = true;
break;
}
}
}
void OnTriggerExit2D(Collider2D collision) {
if (collision.gameObject.tag == "Border") {
switch (collision.gameObject.name) {
case "Top":
isTouchTop = false;
break;
case "Bottom":
isTouchBottom = false;
break;
case "Left":
isTouchLeft = false;
break;
case "Right":
isTouchRight = false;
break;
}
}
}
}
5. 발사체 파워
//Player 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public GameObject bulletObjA;
public GameObject bulletObjB;
Animator anim;
public float speed;
public float power;
public float maxShotDelay;
public float curShotDelay;
public bool isTouchTop;
public bool isTouchBottom;
public bool isTouchLeft;
public bool isTouchRight;
void Awake() {
anim = GetComponent<Animator>();
}
void Update() {
Move();
Fire();
Reload();
}
void Move() { //플레이어 이동 함수
float h = Input.GetAxisRaw("Horizontal");
if ((isTouchRight && h == 1) || (isTouchLeft && h == -1))
h = 0;
float v = Input.GetAxisRaw("Vertical");
if ((isTouchTop && v == 1) || (isTouchBottom && v == -1))
v = 0;
Vector3 curPos = transform.position;
Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;
transform.position = curPos + nextPos;
if (Input.GetButtonDown("Horizontal") || Input.GetButtonUp("Horizontal"))
anim.SetInteger("Input", (int)h);
}
void Fire() { //플레이어 총알 발사 함수
if (!Input.GetButton("Fire1")) //Ctrl 키
return;
if (curShotDelay < maxShotDelay) //장전 중이라면
return;
switch(power) {
case 1:
GameObject bullet = Instantiate(bulletObjA, transform.position, transform.rotation);
Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
rigid.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
break;
case 2:
GameObject bulletR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.1f, transform.rotation);
GameObject bulletL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.1f, transform.rotation);
Rigidbody2D rigidR = bulletR.GetComponent<Rigidbody2D>();
Rigidbody2D rigidL = bulletL.GetComponent<Rigidbody2D>();
rigidR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
rigidL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
break;
case 3:
GameObject bulletRR = Instantiate(bulletObjA, transform.position + Vector3.right * 0.35f, transform.rotation);
GameObject bulletCC = Instantiate(bulletObjB, transform.position, transform.rotation);
GameObject bulletLL = Instantiate(bulletObjA, transform.position + Vector3.left * 0.35f, transform.rotation);
Rigidbody2D rigidRR = bulletRR.GetComponent<Rigidbody2D>();
Rigidbody2D rigidCC = bulletCC.GetComponent<Rigidbody2D>();
Rigidbody2D rigidLL = bulletLL.GetComponent<Rigidbody2D>();
rigidRR.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
rigidCC.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
rigidLL.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
break;
}
curShotDelay = 0;
}
void Reload() {
curShotDelay += Time.deltaTime;
}
void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.tag == "Border") {
switch(collision.gameObject.name) {
case "Top":
isTouchTop = true;
break;
case "Bottom":
isTouchBottom = true;
break;
case "Left":
isTouchLeft = true;
break;
case "Right":
isTouchRight = true;
break;
}
}
}
void OnTriggerExit2D(Collider2D collision) {
if (collision.gameObject.tag == "Border") {
switch (collision.gameObject.name) {
case "Top":
isTouchTop = false;
break;
case "Bottom":
isTouchBottom = false;
break;
case "Left":
isTouchLeft = false;
break;
case "Right":
isTouchRight = false;
break;
}
}
}
}
'유니티 프로젝트 > 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 종스크롤 슈팅: 적 비행기 만들기[B29] (0) | 2022.02.28 |
[Unity/유니티] 기초-2D 종스크롤 슈팅: 플레이어 이동 구현하기[B27] (0) | 2022.02.26 |