주녘공부일지

[ShootingGame] 2. 총알 발사 구현하기 본문

GameEngine/Unity - Lecture

[ShootingGame] 2. 총알 발사 구현하기

주녘 2022. 1. 26. 19:58
728x90

Player.cs
0.00MB
Bullet.cs
0.00MB

 

1. 프리팹 생성

총알 프리팹을 만들고 에셋에 Prefabs 폴더를 만들어서 프리팹들을 관리

- 충돌할 범위를 콜라이더로 설정하고 충돌감지를 위해 리지드바디2D 추가하고 물리 연산 타입을 Kinematic으로 처리할 것이기 때문에 Is Trigger 체크

+ Resources.Load()를 이용할 Prefabs이라면 Resources 폴더에 넣어야 함

https://godgjwnsgur7.tistory.com/7?category=986022

 

[Unity] Prefab이란?

프리팹(Prefab) : 게임 오브젝트 -> 재사용 가능한 에셋 씬에 올라온 오브젝트에 접근할 수 없는데, 이를 하기 위해서는 인스턴스화된 오브젝트를 게임매니저에게 가지게 하여 프리팹의 오브젝트

godgjwnsgur7.tistory.com

 

2. 발사체 제거 경계

class Bullet - Player Bullet 프리팹들의 컴포넌트로 추가

 

- Tag를 활용해 BorderBullet을 만들고 OnTriggerEnter2D로 발사체가 BorderBullet Tag를 가진 오브젝트에 충돌할 시에 오브젝트를 삭제하는 로직 // Collider의 Is Trigger 체크

- BorderBullet Tag 추가

 

3. 발사체 생성, 발사체 파워 설정

class Player - 총알 프리팹을 저장할 변수 생성하고 프리팹을 하이어라키창에 드래그하여 집어넣음
class Player

 

- Reload() : 발사 딜레이를 설정하는 재장전 함수 // Update()문에서 실행

- Fire() : 맨 위의 if문 조건 완료 시 밑에 switch문 로직 실행되어 총알을 발사하고 발사 후엔 딜레이 시간 0으로 초기화 // Update()문에서 실행

 

+ Instantiate(GameObject original ,Vector3 position ,Quaternion rotation)

- Instantiate(생성할 오브젝트, 생성할 위치, 회전 값);

 + 회전 값(3번째 인자)에 Quaternion.identity 입력 시, 회전x

https://docs.unity3d.com/kr/530/ScriptReference/Object.Instantiate.html

 

Unity - 스크립팅 API: Object.Instantiate

This function makes a clone of an object in a similar way to the Duplicate command in the editor. If you are cloning a GameObject, or something attached to a GameObject, then you can also optionally specify its position and rotation. If you are cloning a C

docs.unity3d.com

+ AddForce(Vector3 force, ForceMode)

- AddForce(방향*힘 값, 힘의 종류 ForceMode)

- rigidbody에 힘을 가하여 이동시키는 로직

https://godgjwnsgur7.tistory.com/10

 

[Unity] ForceMode(ForceMode2D)의 종류

Force : 연속 - 현실적인 물리현상 Impulse : 연속 + 질량 무시 - 오브젝트의 질량에 관계없이 이동 Acceleration : 불연속 - 짧은 순간의 힘, 충돌, 폭발, 등에 사용 VelocityChange : 불연속 + 질량 무시 - 질량..

godgjwnsgur7.tistory.com

 

https://github.com/godgjwnsgur7/ShootingGame

 

GitHub - godgjwnsgur7/ShootingGame

Contribute to godgjwnsgur7/ShootingGame development by creating an account on GitHub.

github.com

 

 

강의 출처

https://www.youtube.com/watch?v=JUG0GnsJHQw&list=PLO-mt5Iu5TeYtWvM9eN-xnwRbyUAMWd3b&index=2 

 

728x90