티스토리 뷰
아군 비행기
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Friendly_ship : MonoBehaviour
{
public float moveSpeed = 5.0f;
public bool canShoot = true;
public float shootDelay;
public float shootTimer = 0.0f;
public VariableJoystick joystick;
private Camera _mainCam;
public Entity _entity;
// Start is called before the first frame update
void Start()
{
_mainCam = Camera.main; //유니티 생성시 만들어지는 기본 카메라
_entity = GetComponent<Entity>();
_entity.isPlayer = true;
}
// Update is called once per frame
void Update()
{
//카메라 외부로 나갈 수 X
Vector3 pos = _mainCam.WorldToViewportPoint(transform.position); //메인카메라를 뷰포트값으로 설정 (좌측하단 0,0 우측하단 1,1)
if (pos.x < 0f) pos.x = 0f;
if (pos.x > 1f) pos.x = 1f;
if (pos.y < 0f) pos.y = 0f;
if (pos.y > 1f) pos.y = 1f;
transform.position = _mainCam.ViewportToWorldPoint(pos);
//이동
MoveControl();
}
private void MoveControl()
{
float xInput = Input.GetAxis("Horizontal") + joystick.Horizontal;
float distanceX = xInput * Time.deltaTime * moveSpeed;
this.gameObject.transform.Translate(distanceX, 0, 0);
float yInput = Input.GetAxis("Vertical") + joystick.Vertical;
float distanceY = yInput * Time.deltaTime * moveSpeed;
this.gameObject.transform.Translate(0, distanceY, 0);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.TryGetComponent(out enemy hit))
{
_entity.TakeDamage(1);
hit.entity.TakeDamage(hit.entity.maxHealth);
}
if (other.TryGetComponent(out bullet hitf))
{
if (!hitf.isPlayer)
{
_entity.TakeDamage(1);
Destroy(hitf.gameObject);
}
}
}
}
총알
using UnityEngine;
public class bullet : MonoBehaviour
{
public float moveSpeed = 10.0f;
public bool isPlayer;
void Update()
{
transform.position += transform.right * (moveSpeed * Time.deltaTime);
Destroy(gameObject, 8f);
}
}
아군 총알 발사
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Fire : MonoBehaviour
{
public bullet bulletFactory; //bullet 형식의 원본(?)
public GameObject firePosition; //쏘는 위치
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Fire();
}
}
public void Fire()
{
bullet bulletInst = Instantiate(bulletFactory);
bulletInst.transform.position = firePosition.transform.position; //총알의 초기위치
bulletInst.isPlayer = true;
Destroy(bulletInst, 8f); //8초 이후 삭제
}
}
체력 - 아군, 적군, 보스 모두 사용할수 있도록 되어있다.
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
//using Debug = System.Diagnostics.Debug;
public class Entity : MonoBehaviour
{
public int maxHealth = 2;
public int _curHealth;
public bool isEnemy;
public bool isBoss;
public bool isPlayer;
public TextScore score;
void Start()
{
_curHealth = maxHealth;
}
public void TakeDamage(int damage)
{
_curHealth -= damage;
if (_curHealth <= 0)
{
Death();
}
}
private void Death()
{
if(isPlayer)
{
SceneManager.LoadScene("Udie");
}
else if (isBoss)
{
SceneManager.LoadScene("Clear");
}
else
{
Destroy(gameObject);
}
}
}
플레이어 체력
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player_HP : MonoBehaviour
{
public Slider hpBar;
public Entity _player;
void Update()
{
hpBar.value = (float)_player._curHealth / _player.maxHealth;
}
}
적기
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
public class enemy : MonoBehaviour
{
public Entity entity;
public float moveSpeed = 1.5f;
void Start()
{
entity = GetComponent<Entity>();
entity.isEnemy = true;
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.right * (moveSpeed * Time.deltaTime));
if(transform.position.x < -10)
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.TryGetComponent(out bullet hit))
{
if (hit.isPlayer)
{
entity.TakeDamage(1);
Destroy(hit.gameObject);
}
}
}
}
적기 총알 발사
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_Fire : MonoBehaviour
{
public bullet bulletPrefab;
private Transform player;
// Start is called before the first frame update
void Start()
{
if (Random.Range(0f, 1f) < 0.95f)
{
player = FindObjectOfType<Friendly_ship>().transform;
Invoke(nameof(Fire), Random.Range(3f, 5f));
}
}
void Fire()
{
Vector2 dir = player.position - transform.position;
bullet newBullet = Instantiate(bulletPrefab);
newBullet.transform.position = transform.position;
newBullet.transform.right = dir;
if(Random.Range(0, 1) == 0)
{
newBullet.transform.rotation = transform.rotation;
}
newBullet.isPlayer = false;
Destroy(newBullet.gameObject, 8f);
}
}
적기 생성
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using TMPro;
using UnityEngine;
using Random = UnityEngine.Random;
public class EnemyFacto : MonoBehaviour
{
public float randomX;
public float randomY;
public bool enableSpawn = true;
public enemy enemyFactory;
public TextScore score;
void SpawnEnemy()
{
randomX = Random.Range(10f, 15f);
randomY = Random.Range(-5f, 5f);
if (enableSpawn)
{
enemy enemyInst = Instantiate(enemyFactory);
enemyInst.transform.position = new Vector3(randomX, randomY, 0.0f);
enemyInst.moveSpeed = Random.Range(1f, 3f);
}
}
private void Start()
{
InvokeRepeating("SpawnEnemy", 5, 1);
}
void Update()
{
}
}
보스
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
public class Boss : MonoBehaviour
{
public bullet bulletPrefab;
public bullet bigBulletPrefab;
public enemy enemyPrefab;
private Transform player;
public Vector2 patternIntervalRange;
private Entity _entity;
IEnumerator Circle_Fire(int startAngle, float fireRate, int angleRate, float speed)
{
float fireAngle = startAngle;
for (int i = 0; i < 360 / angleRate; i++)
{
bullet newBullet = Instantiate(bulletPrefab);
newBullet.moveSpeed = speed;
newBullet.transform.position = transform.position;
Vector2 direction = new Vector2(Mathf.Cos(fireAngle * Mathf.Deg2Rad), Mathf.Sin(fireAngle * Mathf.Deg2Rad));
newBullet.transform.right = direction;
fireAngle += angleRate;
if (fireAngle > 360) fireAngle -= 360; // 0~360
newBullet.isPlayer = false;
yield return new WaitForSeconds(fireRate);
}
}
IEnumerator Circle_Enemy(int startAngle, float fireRate, int angleRate, float speed)
{
float enemyAngle = startAngle;
for (int i = 0; i < 360 / angleRate; i++)
{
enemy newEnemy = Instantiate(enemyPrefab);
newEnemy.moveSpeed = speed;
newEnemy.transform.position = transform.position;
Vector2 direction = new Vector2(Mathf.Cos(enemyAngle * Mathf.Deg2Rad), Mathf.Sin(enemyAngle * Mathf.Deg2Rad));
enemyAngle += angleRate;
enemyPrefab.transform.right = direction;
if (enemyAngle > 360) enemyAngle -= 360; // 0~360
yield return new WaitForSeconds(fireRate);
}
}
// Start is called before the first frame update
void Start()
{
player = FindObjectOfType<Friendly_ship>().transform;
_entity = GetComponent<Entity>();
_entity.isBoss = true;
StartCoroutine(PatternManager());
}
IEnumerator Pattern1()
{
StartCoroutine(Circle_Fire(15, 0f, 30, 4f));
yield return StartCoroutine(Circle_Fire(0, 0.1f, 30, 6f));
for (int i = 0; i < 2; i++)
{
StartCoroutine(Circle_Fire(7, 0f, 15, 5f));
yield return StartCoroutine(Circle_Fire(0, 0.05f, 15, 7f));
}
}
IEnumerator Pattern2()
{
for (int i = 0; i < 20; i++)
{
Vector2 dir = player.position - transform.position;
bigBulletPrefab = Instantiate(bigBulletPrefab);
bigBulletPrefab.moveSpeed = 13f;
bigBulletPrefab.transform.position = transform.position;
bigBulletPrefab.transform.right = dir;
yield return new WaitForSeconds(0.5f);
}
}
IEnumerator Pattern3()
{
StartCoroutine(Circle_Enemy(15, 0f, 30, 4f));
yield return StartCoroutine(Circle_Enemy(0, 0.1f, 30, 6f));
}
IEnumerator PatternManager()
{
while (true)
{
yield return new WaitForSeconds(Random.Range(patternIntervalRange.x, patternIntervalRange.y));
yield return StartCoroutine(Pattern1());
yield return new WaitForSeconds(Random.Range(patternIntervalRange.x, patternIntervalRange.y));
yield return StartCoroutine(Pattern2());
yield return new WaitForSeconds(Random.Range(patternIntervalRange.x, patternIntervalRange.y));
yield return StartCoroutine(Pattern3());
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.TryGetComponent(out bullet hit))
{
if (hit.isPlayer)
{
_entity.TakeDamage(1);
Destroy(hit.gameObject);
}
}
}
}
보스 스폰
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossSpawn : MonoBehaviour
{
public EnemyFacto factory;
public Boss bossPrefab;
void Start()
{
Invoke(nameof(SpawnBoss), 50f);
}
void SpawnBoss()
{
factory.enableSpawn = false;
Instantiate(bossPrefab, transform.position, transform.rotation);
}
}
게임 시작 씬넘기기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class StartGame : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene("Game");
}
}
}
재시작
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RestartGame : MonoBehaviour
{
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene("MainScreen");
}
}
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- python
- 더하기
- 나머지
- 넓이
- 👼
- c++
- 괄호
- 직업군 추천
- programmers
- forensic
- 카카오 2021 블라인드 테스트
- 스택
- HackCTF
- FIBO
- 쇠막대기
- 1
- 4
- 21758
- openCV
- 7567
- 2
- 피보나치
- 파이썬
- web
- boj
- 백준
- 다익스트라
- 꿀따기
- math
- 프로그레머스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함