asd

카테고리 없음 2016. 5. 16. 13:06

*******레이저와 적기가 만났을때 폭발효과를 내며 파괴됨 그리고 레이저가 scene밖으로 나갔을때 자동으로 삭제됨

1.레이저와 적기가 만났을때 폭발효과를 내며 파괴됨
1-1.enemy.css에 이 코드를 추가
void OnTriggerEnter2D(Collider2D col) 

//print(col.gameObject.name); 
if (col.gameObject.tag == "Laser") //Laser라는 태그가달린 오브젝트와 부딪히면

Instantiate(explosionPrefab, transform.position, Quaternion.identity); 
 // 현재위치 현재 방향에 explosionPrefab를 재생
Destroy(col.gameObject);    //부딪힌 게임오브젝트를 파괴
Destroy(this.gameObject);  //이 게임오브젝트를 파괴

}
2-2 

2.레이저가 scene밖으로 나갔을떄 삭제
2-1.laser.css에 이 코드 추가
void OnBecameInvisible() 
//유니티에서 이 오브젝트가 scene밖으로 나갔을떄 자동으로 호출하는 함수

Destroy(this.gameObject);   //이 게임 오브젝트 파괴
}

********폭파 효과음내기
1.Hieaechy에 빈 오브젝트 만들기고 soundManager로 이름바꾸기 
유니티 상단에  gamedbject --> create empty
2.soundManager에 AudioSource추가 
유니티 상단에 component --> Audio --> AudioSource
3.soundManager.css스크립트 만들기
4.코드 넣기
using UnityEngine;
using System.Collections;

public class soundManager : MonoBehaviour { 
public AudioClip soundExplosion;   //Audioclip이라는 데이터타입에 변수생성
AudioSource myAudio; //컴퍼넌트에서 AudioSource가져오기
public static soundManager instance; //다른 스크립트에서 이스크립트에있는 함수를 호출할때 쓰임

void Awake()  // Start함수보다 먼저 호출됨

if (soundManager.instance == null)  //게임시작했을때 이 instance가 없을때
soundManager.instance = this;  // instance를 생성
}
// Use this for initialization
void Start () { 
myAudio = GetComponent<AudioSource>();  //myAudio에 컴퍼넌트에있는 AudioSource넣기


public void PlaySound() 

myAudio.PlayOneShot(soundExplosion); 
// 유니티에서 기본으로 제공하는 함수 (이 소리)를 한번재생
}

// Update is called once per frame
void Update () {

}
}
player스크립트 -> void OnTriggerEnter2D(Collider2D col)이곳에
soundManager.instance.PlaySound();이거 추가



WRITTEN BY
Who1sth1s

,