유니티/수업 내용
2D슈팅 - 적기A, B 애니메이션, 피격연출, 등장패턴추가
hyunjin-dev-log
2022. 1. 18. 18:44
적기는 일정하게 움직인다
기체와 미사일은 화면밖으로 나가면 사라진다
기체들과 기체들이 쏘는 미사일은 충돌판정이 있다
적기는 종류에 따라 체력을 가지고있고 체력이 0이되면 폭파되서 사라진다
적기와 충돌한 미사일도 사라진다
적기 또는 적기가 쏜 미사일과 충돌한 플레이어기체도 사라진다
시간의 흐름에 따라 적기가 등장한다
적기A는 총알을 발사 할수도 있다
적기B는 빠른속도로 내려온다
충돌판정관련해서 처음에 화살피하기를 참고했는데 GameObject.Find 를 Update메서드에서 계속해도
또다른 오브젝트를 감지를 못해서 헤매다가 기억하는 콜라이더와 리지바디만으로는 안되서
검색을 통해 다음 블로그에서 https://fiftiesstudy.tistory.com/205
잊고있었던 트리거를 떠올렸다
플레이어기체는 제한된 구역내에서만 움직일수있다
기체들은 폭파될때 그자리에서 폭파애니메이션을 재생한다
플레이어기체도 폭파될때 이동키를 눌러도 컨트롤되지않고
제자리에서 폭파애니메이션을 재생한다
더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private bool isPressLeftArrow;
private bool isPressRightArrow;
private Animator animator;
private int prevKey = 0; // -1 : left, 1 : right
private bool isAllive = true;
private bool isShoot;
private float delta; //총알 발사시 딜레이 누적시간
public GameObject bulletPrefab;
public Transform bulletPoint;
public float span = 0.2f;
public float speed = 2.0f;
// Start is called before the first frame update
void Start()
{
this.animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (isAllive)
{
//플레이어기체 이동
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
this.isPressLeftArrow = true;
if (this.isPressRightArrow)
{
prevKey = 1;
this.isPressRightArrow = false;
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
this.isPressRightArrow = true;
if (this.isPressLeftArrow)
{
prevKey = -1;
this.isPressLeftArrow = false;
}
}
if (Input.GetKeyUp(KeyCode.LeftArrow))
{
this.isPressLeftArrow = false;
if (this.prevKey == 1)
{
this.isPressRightArrow = true;
}
this.prevKey = 0;
}
if (Input.GetKeyUp(KeyCode.RightArrow))
{
this.isPressRightArrow = false;
if (this.prevKey == -1)
{
this.isPressLeftArrow = true;
}
this.prevKey = 0;
}
//플레이어기체 이동가능구역 제한
if (this.transform.position.x < -1.95f)
{
this.transform.position = new Vector3(-1.95f, this.transform.position.y, 0);
}
if (this.transform.position.x > 1.95f)
{
this.transform.position = new Vector3(1.95f, this.transform.position.y, 0);
}
if (this.transform.position.y < -4.55f)
{
this.transform.position = new Vector3(this.transform.position.x, -4.55f, 0);
}
if (this.transform.position.y > 4.5f)
{
this.transform.position = new Vector3(this.transform.position.x, 4.5f, 0);
}
this.animator.SetBool("IsPressLeftArrow", this.isPressLeftArrow);
this.animator.SetBool("IsPressRightArrow", this.isPressRightArrow);
//float v = Input.GetAxis("Vertical"); //키보드 입력을 -1... 0 ... 1 까지 수로 반환 (소수점)
//float h = Input.GetAxis("Horizontal");
float v = Input.GetAxisRaw("Vertical"); //키보드 입력을 -1, 0, 1까지 수로 반환 (정수)
//float h = Input.GetAxisRaw("Horizontal");
int h = 0;
if (this.isPressLeftArrow)
{
h = -1;
}
else if (this.isPressRightArrow)
{
h = 1;
}
else
{
h = 0;
}
if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.RightArrow))
{
if (v != 0)
{
h = 0;
}
}
Vector2 dir = new Vector2(h, v);
this.transform.Translate(dir * this.speed * Time.deltaTime);
//if (this.isPressLeftArrow)
//{
// //방향 * 속도 * 시간
// this.transform.Translate(Vector2.left * 1.0f * Time.deltaTime);
//}
//if (this.isPressRightArrow)
//{
// this.transform.Translate(Vector2.right * 1.0f * Time.deltaTime);
//}
//if (this.isPressUpArrow)
//{
// // 0, 1
// this.transform.Translate(Vector2.up * 1.0f * Time.deltaTime);
//}
//if (this.isPressDownArrow)
//{
// // 0, -1
// this.transform.Translate(Vector2.down * 1.0f * Time.deltaTime);
//}
//총알 발사
if (this.isShoot)
{
this.delta += Time.deltaTime;
if (this.delta > this.span)
{
this.Shoot();
this.delta = 0;
}
}
}
}
private void Shoot()
{
var bulletGo = Instantiate(this.bulletPrefab);
bulletGo.transform.position = this.bulletPoint.position;
}
public void StartShoot()
{
this.isShoot = true;
}
public void StopShoot()
{
this.isShoot = false;
}
private void Die()
{
Destroy(this.gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Enemy") || collision.gameObject.CompareTag("EnemyBullet"))
{
this.isAllive = false;
this.animator.SetTrigger("CrashTrigger");
}
if (collision.gameObject.CompareTag("Coin"))
{
GameObject Director = GameObject.Find("Director");
Director.GetComponent<Director>().score += 10;
}
}
}
피격연출 추가.
시간별 적기등장패턴 추가