게임개발/게임 클라이언트 프로그래밍
ShootingSpace (3) 폭발 효과 만들기
pudding81
2024. 2. 4. 10:58
폭발 애니메이션 생성
: 루프타임 해제 -> 한번만 재생
폭발 컨트롤러 스크립트 작성
생성시 자동 삭제 => 프리팹 만들기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExplosionController : MonoBehaviour
{
void Start()
{
Destroy(gameObject, 0.8f); // 생성뒤 없어지게
}
}
총알 컨트롤러에 게임오브젝트 (프리팹) 붙이기
충돌시 생성 코딩 추가
Instantiate(explosion, this.transform.position, Quaternion.identity); // 게임오브젝트 , 위치 , 회전
게임 오브젝트를 현재 오브젝트의 위치에 기본 회전으로 복제하는 코드
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class BulletController : MonoBehaviour
{
public float speed=4f;
private Vector3 pos;
public GameObject explosion;
void Update()
{
// 위로 총알 발사
this.transform.Translate(Vector2.up * this.speed * Time.deltaTime);
//화면 밖으로 나가면 사라지게
if (this.transform.position.y > 6.56f)
{
Destroy(this.gameObject);
}
}
public void OnTriggerEnter2D(Collider2D collision)
{
//적이랑 충돌하면 사라짐
if (collision.tag == "Enemy") //태그 설정
{
Destroy(collision.gameObject); //적 소멸
Destroy(this.gameObject); //총알 소멸
//폭발 프리팹 설정
Instantiate(explosion,this.transform.position, Quaternion.identity);
}
}
}