총알에 collider2D - istrigger
적에 collider2D , rigidbody2D , Enemy tag
BulletController
private void OnTriggerEnter2D(Collider2D collision)
{
//적이랑 충돌하면 사라짐
if (collision.tag == "Enemy") //태그 설정
{
Destroy(collision.gameObject); //적 소멸
Destroy(this.gameObject); //총알 소멸
}
}
SpaceEnemyController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpaceEnemyController : MonoBehaviour
{
[SerializeField] private float speed = 1f;
void Update()
{
//방향 * 속도 * 시간
Vector3 movement = Vector3.down * speed * Time.deltaTime;
this.transform.Translate(movement);
//현재 y 좌표가 -5보다 작아졌을때 씬에서 제거한다
if (this.transform.position.y <= -5f)
{
Destroy(this.gameObject); //게임오브젝트를 씬에서 제거
}
}
}
적 프리팹 생성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpaceEnemyGenerator : MonoBehaviour
{
public GameObject enemyPrefab;
private float delta; //경과된 시간 변수
void Update()
{
delta += Time.deltaTime; //이전 프레임과 현재 프레임 사이 시간
if (delta > 3) //3초보다 크다면
{
//생성
GameObject go = UnityEngine.Object.Instantiate(this.enemyPrefab);
//위치 재 설정
float randX = UnityEngine.Random.Range(-2.5f, 2.5f);
float randY = UnityEngine.Random.Range(4f, 6.5f);
go.transform.position
= new Vector3(randX, randY, go.transform.position.z);
delta = 0; //경과 시간을 초기화
}
}
}
'게임개발 > 게임 클라이언트 프로그래밍' 카테고리의 다른 글
ShootingSpace (3) 폭발 효과 만들기 (0) | 2024.02.04 |
---|---|
[참조] 폭발효과만들기 (0) | 2024.02.04 |
ShootingSpace(1) 우주선 총알 발사 (0) | 2024.02.03 |
3D 밤송이를 날려라 (0) | 2024.02.03 |
PirateBomb (2) Captain (0) | 2024.02.01 |