충돌 감지후 스파크

 

void OnCollisionEnter(Collision col)   {
  if(col.gameObject.tag == "Bullet")
  
  {
         hit++;
         audio.PlayOneShot(firstSound,1.0f);      

GameObject newSpark1 =

          (GameObject)Instantiate(prefab1,col.transform.position,Quaternion.identity);   
   
          Destroy (col.gameObject);      

          Destroy (newSpark1,0.5f);     
  }
  
  if(hit == 3)
  {
          GetComponent<CapsuleCollider>().enabled = false;   

          Explosion();
  }
 } 

 

 

AddComponent, AddForce

 void Explosion()
 {
  audio.PlayOneShot(secondSound,1.0f);
          //<rigidbody> is used to make phishical effect!!
  GameObject newSpark2 = Instantiate(prefab2,transform.position,Quaternion.identity)as GameObject;
  
  
  gameObject.AddComponent<Rigidbody>();
  GetComponent<Rigidbody>().AddForce(Vector3.up*1000.0f);
  
  Destroy(newSpark2,2.0f);
  Destroy(gameObject,2.5f);
  
 }

 

 

rigidbody.AddForce(Vector3.forward * Speed);

 

 

Gizmo

 public class MyGizmo : MonoBehaviour {

 public Color _color = Color.yellow;
 public float _radius = 0.2f;
 
 void OnDrawGizmos()
 {
        Gizmos.color = _color;
        Gizmos.DrawSphere(transform.position,_radius);
 }
}

 

 

멀티쓰레드, 총알발사

 if(Input.GetButtonUp("Fire1"))
  {
            StartCoroutine("Fire");
  }

 

 

IEnumerator Fire()
 {                  // sound, where? , how big?
      AudioSource.PlayClipAtPoint(BulletSound,firepos.position,1.0f);    

      GameObject newBullet = (GameObject)  

                      Instantiate(BulletPrefab,firepos.position,Quaternion.identity);
       Destroy(newBullet, 3.0f);
       yield return null;     // Do next Frame~!!
 }

 

 

foreach , SendMessage

 foreach(GameObject MonsterMecanim in GameObject.FindGameObjectsWithTag("Monster")) 

  {
             MonsterMecanim.SendMessage("YouWin");   //cal method
  }
  

 

Rotate

 gameObject.transform.Rotate(Vector3.up * Speed * Time.deltaTime);

 

OnTriggerEnter, OnTiriggerExit

 public Light streetLight;
 public AudioClip LightSound;
 
 
 void OnTriggerEnter(Collider hit)  //catch conflict without phishical effet!
 {
  if(hit.gameObject.tag == "Player")
  {
        audio.PlayOneShot(LightSound,1.0f);
        streetLight.light.enabled = true;
  }
 }
 
 void OnTriggerExit(Collider hit)  //catch conflict without phishical effet!
 {
  if(hit.gameObject.tag == "Player")
  {
        audio.PlayOneShot(LightSound,1.0f);
        streetLight.light.enabled = false;
  }
 }

 

 

몬스터생성 (GetComponentsInChildren,Time.time,오브젝트 개수 파악)

 public GameObject Monster;
 public GameObject Zombie;
 public Transform[] point;      //it makes me use other GameObjects. Declare array...
 public float CreateTime = 2.0f;
 

 void Start () {
  point = GameObject.Find ("SpawnPoint").GetComponentsInChildren<Transform>();     //access SpawnPoint and its childrens
 }
 
  void Update () {
  
  if(Time.time > CreateTime)
  {
   if(GameObject.FindGameObjectsWithTag("Monster").Length +      

                             GameObject.FindGameObjectsWithTag("Zombie").Length <=5)
   {
               CreateMonster();
               CreateTime = Time.time + 2.0f;
   }
  }
 
 }// end of Update().
 
 
 void CreateMonster()
 {
            int idx = Random.Range(1,point.Length);
            Instantiate(Monster,point[idx].position,Quaternion.identity);
            Instantiate(Zombie,point[idx].position,Quaternion.identity);
 } //end of CreateMonster().

 

 HpBar

 public Texture ForegroundTexture;
 public Texture BackgroundTexture;
 public Texture2D DamageTexture;  //use 2D UI and change color

 
 void OnGUI()  //draw picture method
 {
  Rect rect = new Rect(10,6,Screen.width/2-20,BackgroundTexture.height);  //(x,y,width,height)
  
  GUI.DrawTexture(rect,BackgroundTexture);
  
  float hp = PlayerCtrl.hp;
  rect.width *= hp;
  
  GUI.color = DamageTexture.GetPixelBilinear(hp,0.5f);   //read color value
  GUI.DrawTexture(rect,ForegroundTexture);
  
  GUI.color = Color.white;
  
  
 }

 

CellPickUp()

 public static int powerCell = 0;
 public AudioClip collectSound;
 public Texture2D[] hudcharge;
 public GUITexture chargeHudGUI;
 
 public Texture2D[] meterCharge;
 public Renderer meter;

 
 void CellPickUp(){
 
  HudOn ();
  AudioSource.PlayClipAtPoint(collectSound,transform.position);
  chargeHudGUI.texture = hudcharge[powerCell];
  meter.material.mainTexture= meterCharge[powerCell];
  powerCell++;
  
  if(powerCell == 4)
  {
   Destroy(GameObject.Find("hud_nocharge"),5.0f); //objectname in hirachy
  }
  
 }
 
 void HudOn(){
  if(!chargeHudGUI.enabled)
  {
   chargeHudGUI.enabled = true;
  }
 }

 

 

'유니티3D > 함수' 카테고리의 다른 글

UNITY3D 자주쓰는 스크립트_3  (0) 2013.09.29
UNITY3D 자주쓰는 스크립트_2  (0) 2013.09.27
Rigidbody.AddForce()  (0) 2013.08.07
C# 스크립트 함수 _2  (0) 2013.07.21
C# 스크립트 심화_1  (0) 2013.07.21

+ Recent posts