본문 바로가기

Unity3D Tank/Explosion

Explosion Effect using Prefab

프리팹을 이용한 포탄 폭발 장면 구성 예


유니티에서 제공하는 폭발 프리팹을 사용하기 위해 아래와 같이 순서대로 선택하여 Small Explosion.prefab 을 프로젝트 뷰로 임포트한다

Assets > Import Package > Particles > Legacy Particles > Small Explosion.prefab > Import


프로젝트 뷰에 임포트된 프리팹을 다음과 같은 절차에 따라서 속성을 편집한다


[Small Explosion.prefab 속성 편집창]



속성 편집을 마친 후에 Hierarchy 뷰에 있는 Small Explosion 을 드래그하여 사용하기 편한 프로젝트 뷰의 Prefabs 폴더에 옮겨 놓고 아래처럼 스크립트의 변수에 할당해준다


[속성 편집을 마친 Small Explosion 프리팹을 드래그하여 스크립트 변수에 할당]




using UnityEngine;

using System.Collections;


public class Cannon : MonoBehaviour {


public GameObject ballPrefab; // 포탄 프리팹


// Use this for initialization

void Start () {

}


/**

* Cannon(Cube, 탱크바디)

*   |- Top(Cube, 포탑)

*       |- Barrel(Cylinder, 포신)

*            |- BallPoint(EmptyObject, 복제된 포탄 인스턴스의 초기위치를 참조할 목적으로 추가함)

*/

// Update is called once per frame

void Update () {

if(Input.GetButtonDown("CannonBall")) { // 스페이스 키 


GameObject ballPoint = GameObject.Find("BallPoint");

GameObject top = GameObject.Find ("Top");

GameObject ba = GameObject.Find ("Barrel");


//포신이 향하고 있는 방향을 구한다

// Quaternion은 회전정보이며 벡터와 곱하면 해당벡터를 회전변환한 결과 벡터가 산출된다

Vector3 dir = ba.transform.rotation * Vector3.up;


// 프리팹 원본 오브젝트를 이용하여 인스턴스를 생성한다

GameObject ball = (GameObject)Instantiate(ballPrefab, ballPoint.transform.position, Quaternion.identity);

//Rigidbody rb =  ball.GetComponent<Rigidbody>();

Rigidbody rb =  ball.AddComponent<Rigidbody>();

// 포신이 향한 방향과 각도를 향하여 포탄 발사

rb.AddForce(dir*2000f);

}

GameObject cannonTop = GameObject.Find("Top");

// 좌우 방향키에 따라서 포탑을 좌우로 회전한다

cannonTop.transform.Rotate(cannonTop.transform.up * Input.GetAxis("Horizontal"), Space.World);


GameObject barrel = GameObject.Find ("Barrel");

// 상하 방향키에 따라서 포신의 상하 각도를 변경한다

barrel.transform.RotateAround(cannonTop.transform.position, barrel.transform.forward,Input.GetAxis ("Vertical")*-1f);

}


void FixedUpdate() {

Move ();

}


void Move() {

//print ("Move");

Rigidbody rb = gameObject.GetComponent<Rigidbody> ();

if(Input.GetKey(KeyCode.Keypad8)) {

print ("전진");

rb.AddForce (transform.forward * 7f);

}

if(Input.GetKey(KeyCode.Keypad6)){

print("우회전");

transform.Rotate (new Vector3(0F,1F,0F)*Time.fixedDeltaTime*20f);

}

if(Input.GetKey(KeyCode.Keypad4)){

print("좌회전");

transform.Rotate (new Vector3(0F,1F,0F)* -Time.fixedDeltaTime*20f);

}

if(Input.GetKey(KeyCode.Keypad2)){

print("후진");

rb.AddForce (transform.forward * -7f);

}

}

}



아래의 코드를 프로젝트 뷰에 있는 ballPrefab 에 콤포넌트로 추가한다. 

할당된 폭발 프리팹을 포탄이 지면에 충돌하는 순간 보여주는 코드이다

using UnityEngine;

using System.Collections;


public class Ball : MonoBehaviour {


public GameObject explosionPrefab; // 프로젝트 뷰에 있는 smallExplosion 프리팹을 드래그하여 할당


// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

}


void OnCollisionEnter(Collision collision) {

ContactPoint contact = collision.contacts[0];

Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);

Vector3 pos = contact.point;

Instantiate (explosionPrefab, pos, rot);

Destroy(gameObject);

print ("충돌");

}

}




[실행 테스트]