일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 가상현실
- meta
- 앱 배포
- input system
- 개발
- 개발일지
- 드래곤 플라이트
- 유니티
- 드래곤 플라이트 모작
- 팀 프로젝트
- 오브젝트 풀링
- 유니티 GUI
- ChatGPT
- 유니티 Json 데이터 연동
- OVR
- 포트폴리오
- CGV
- 오큘러스
- HAPTIC
- VR
- Oculus
- 멀티플레이
- 유니티 UI
- meta xr
- 길건너 친구들
- XR
- 모작
- 팀프로젝트
- Photon Fusion
- 연습
Archives
- Today
- Total
EasyCastleUNITY
2D 탄막 게임 1 본문
1. 플레이어 이동 및 공격(총알 만들기)
2. 적 피격 애니메이션, 사망 애니메이션
BulletGenerator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletGenerator : MonoBehaviour
{
[SerializeField]
private Transform BulletInitPos; //총알 생성 위치
[SerializeField]
private GameObject bulletPrefab; //총알 프리팹
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GameObject go = Instantiate(bulletPrefab);
go.transform.position = this.BulletInitPos.position;
}
}
}
BulletControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletControl : MonoBehaviour
{
private Transform transform;
private float moveSpeed = 3.0f;
// Start is called before the first frame update
void Start()
{
this.transform = this.GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Vector2.up * this.moveSpeed * Time.deltaTime);
this.SelfDestory();
}
private void SelfDestory()
{
if(this.transform.position.y >4.8f)
Destroy(this.gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("<color=yellow>총알 삭제</color>");
Destroy(this.gameObject);
}
}
PlayerControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
enum eState
{
Idle, Left , Right
}
private Transform trans; //트랜스폼
private Animator anim; //애니메이터
[SerializeField]
private float moveSpeed = 2.0f; //이동속도
// Start is called before the first frame update
void Start()
{
this.trans =GetComponent<Transform>();
this.anim = GetComponent<Animator>();
// 0: Idle, 1:Left 2:Right
}
// Update is called once per frame
void Update()
{
this.trans.position = this.ClampPosition(this.trans.position);
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector2 moveDir = (Vector2.up * v) + (Vector2.right * h);
this.trans.Translate(moveDir.normalized*this.moveSpeed*Time.deltaTime);
if (Input.GetKey(KeyCode.LeftArrow))
{
this.PlayAnimation(eState.Left);
}
else if(Input.GetKey(KeyCode.RightArrow)) {
this.PlayAnimation(eState.Right);
}
else
{
this.PlayAnimation(eState.Idle);
}
}
private Vector2 ClampPosition (Vector2 position)
{
return new Vector2(Mathf.Clamp(position.x, -2.3f, 2.3f),
Mathf.Clamp(position.y, -4.5f, 4.5f));
}
private void PlayAnimation(eState state)
{
int istate = (int)state;
this.anim.SetInteger("State", istate);
}
}
EnemyControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyControl : MonoBehaviour
{
private Animator anim;
private Coroutine routine;
public System.Action onDie;
[SerializeField]
private int hp = 5;
// Start is called before the first frame update
void Start()
{
this.anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (this.hp <= 0)
{
this.Die();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("<color=red>적 피격!</color>");
hp--;
this.PlayAnimation();
}
private void SelfDestroy()
{
Destroy(this.gameObject);
}
private void Die()
{
this.onDie();
StartCoroutine(CoDie());
}
private IEnumerator CoDie()
{
yield return new WaitForSeconds(0.167f);
this.SelfDestroy();
yield return null;
}
private void PlayAnimation()
{
if(this.routine != null)
{
this.StopCoroutine(this.routine);
}
this.routine = StartCoroutine(CoPlayAnimation());
}
private IEnumerator CoPlayAnimation()
{
this.anim.SetBool("State", true);
yield return new WaitForSeconds(0.167f);
this.anim.SetBool("State", false);
yield return null;
}
}
GameMain
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameMain : MonoBehaviour
{
[SerializeField]
private GameObject explosionPrefab;
[SerializeField]
private EnemyControl enemy;
// Start is called before the first frame update
void Start()
{
this.enemy.onDie = () =>{
GameObject go = Instantiate(explosionPrefab);
go.transform.position = enemy.transform.position;
this.RemoveExplosion(go);
};
}
// Update is called once per frame
void Update()
{
}
private void RemoveExplosion(GameObject go)
{
StartCoroutine(this.CoRemoveExplosion(go));
}
private IEnumerator CoRemoveExplosion(GameObject go)
{
yield return new WaitForSeconds(0.5f);
Destroy(go);
yield return null;
}
}
'유니티 심화' 카테고리의 다른 글
[과제]궁수의 전설 이동 및 공격 만들기(2023.08.23 00:01분 최종완성) (0) | 2023.08.19 |
---|---|
3D Space Shooter Test1 (0) | 2023.08.18 |
Space Shooter 카메라 Player Follow (0) | 2023.08.18 |
2D 탄막 게임 테스트 (적 피격 애니메이션 및 사망 애니메이션) (0) | 2023.08.18 |
3D SpaceShooter Player Control & FollowCam (0) | 2023.08.18 |