일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 2월
- 유니티
- 6월
- 개인 프로젝트
- 2022년
- 2024년
- 코딩 테스트
- C/C++
- 입문
- 자료 구조
- 2025년
- 코딩 기초 트레이닝
- 3월
- 10월
- 기초
- 2023년
- 백준
- 4월
- 골드메탈
- c++
- 개인 프로젝트 - 런앤건
- 단계별로 풀어보기
- 5월
- 프로그래머스
- 게임 엔진 공부
- 다이나믹 프로그래밍
- 수학
- 유니티 심화과정
- 1월
- todolist
- Today
- Total
기록 보관소
[Unity/유니티] 기초-3D 쿼터뷰 액션 게임: 피격 테스터 만들기[B46] 본문
개요
유니티 입문과 독학을 위해서 아래 링크의 골드메탈님의 영상들을 보며 진행 상황 사진 또는 캡처를 올리고 배웠던 점을 요약해서 적는다.
현재는 영상들을 보고 따라하고 배우는 것에 집중할 것이며, 영상을 모두 보고 따라한 후에는 개인 프로젝트를 설계하고 직접 만드는 것이 목표다.
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
3D 쿼터뷰 액션 게임: 피격 테스터 만들기[B46]
1. 오브젝트 생성
2. 충돌 이벤트
//Bullet 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
public int damage;
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Floor") //탄피
Destroy(gameObject, 3); //3초 뒤에 사라지기
}
void OnTriggerEnter(Collider other) { //총알을 isTrigger로 바꾸었으므로
if(other.gameObject.tag == "Wall") //총알
Destroy(gameObject);
}
}
//Enemy 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public int maxHealth;
public int curHealth;
Rigidbody rigid;
BoxCollider boxCollider;
void Awake() {
rigid = GetComponent<Rigidbody>();
boxCollider = GetComponent<BoxCollider>();
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Melee") {
Weapon weapon = other.GetComponent<Weapon>();
curHealth -= weapon.damage;
Debug.Log("Range : " + curHealth);
}
else if (other.tag == "Bullet") {
Bullet bullet = other.GetComponent<Bullet>();
curHealth -= bullet.damage;
Debug.Log("Range : " + curHealth);
}
}
}
- 추가적으로, Weapon 스크립트 파일에서 근접 무기를 처리하는 코루틴 함수가 0.3초동안만 콜라이더를 활성화 시켜서 정상적으로 처리되지 않았다. 그래서 콜라이더와 이펙트를 0.4초로 살짝 늘려주었더니 위 캡처처럼 처리되었다.
3. 피격 로직
//Enemy 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public int maxHealth;
public int curHealth;
Rigidbody rigid;
BoxCollider boxCollider;
Material mat;
void Awake() {
rigid = GetComponent<Rigidbody>();
boxCollider = GetComponent<BoxCollider>();
mat = GetComponent<MeshRenderer>().material;
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Melee") {
Weapon weapon = other.GetComponent<Weapon>();
curHealth -= weapon.damage;
StartCoroutine(OnDamage());
}
else if (other.tag == "Bullet") {
Bullet bullet = other.GetComponent<Bullet>();
curHealth -= bullet.damage;
StartCoroutine(OnDamage());
}
}
IEnumerator OnDamage() {
mat.color = Color.red;
yield return new WaitForSeconds(0.1f);
if (curHealth > 0) {
mat.color = Color.white;
}
else {
mat.color = Color.gray;
gameObject.layer = 12; //Enemy Dead 레이어로 변경
Destroy(gameObject, 4);
}
}
}
- Material은 GetComponent<Material>로 할 수 없고, Mesh Renderer 컴포넌트를 통해서 접근이 가능하다. 그래서 GetComponent<MeshRenderer>().material;로 선언해야한다.
4. 넉백 추가
//Enemy 스크립트 파일
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public int maxHealth;
public int curHealth;
Rigidbody rigid;
BoxCollider boxCollider;
Material mat;
void Awake() {
rigid = GetComponent<Rigidbody>();
boxCollider = GetComponent<BoxCollider>();
mat = GetComponent<MeshRenderer>().material;
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Melee") {
Weapon weapon = other.GetComponent<Weapon>();
curHealth -= weapon.damage;
Vector3 reactVec = transform.position - other.transform.position;
StartCoroutine(OnDamage(reactVec));
}
else if (other.tag == "Bullet") {
Bullet bullet = other.GetComponent<Bullet>();
curHealth -= bullet.damage;
Vector3 reactVec = transform.position - other.transform.position;
Destroy(other.gameObject);
StartCoroutine(OnDamage(reactVec));
}
}
IEnumerator OnDamage(Vector3 reactVec) {
mat.color = Color.red;
yield return new WaitForSeconds(0.1f);
if (curHealth > 0) {
mat.color = Color.white;
}
else {
mat.color = Color.gray;
gameObject.layer = 12; //Enemy Dead 레이어로 변경
reactVec = reactVec.normalized;
reactVec += Vector3.up;
rigid.AddForce(reactVec * 5, ForceMode.Impulse);
Destroy(gameObject, 4);
}
}
}
'유니티 프로젝트 > 3D 쿼터뷰 액션게임' 카테고리의 다른 글
[Unity/유니티] 기초-3D 쿼터뷰 액션 게임: 목표를 추적하는 AI 만들기[B48] (0) | 2022.04.02 |
---|---|
[Unity/유니티] 기초-3D 쿼터뷰 액션 게임: 수류탄 구현하기[B47] (0) | 2022.04.02 |
[Unity/유니티] 기초-3D 쿼터뷰 액션 게임: 플레이어 물리 문제 고치기[B45] (0) | 2022.03.27 |
[Unity/유니티] 기초-3D 쿼터뷰 액션 게임: 원거리 공격 구현[B44] (0) | 2022.03.26 |
[Unity/유니티] 기초-3D 쿼터뷰 액션 게임: 코루틴으로 근접공격 구현하기[B43] (0) | 2022.03.26 |