네비게이션, GetComponent, Animation, BloodEffect, BloodPanel, SetBool

 using UnityEngine;
using System.Collections;

[RequireComponent (typeof(AudioSource))]     //i don't need to Input Audio sources again in component.
public class MonsterCtrl : MonoBehaviour {
 
 public Transform PlayerTr;
 public Transform MonsterTr;
 public NavMeshAgent nv;
 public float TraceTime = 0.5f;
 public Animator anim;
 public int hp = 100;
 public AudioClip killSound;
 bool isdead = false;
 private int hitID;
 public GameObject BloodEffect;
 public GameObject BloodPanel;
 
 // Use this for initialization01037141701
 void Start () {
  PlayerTr = GameObject.Find ("Player").GetComponent<Transform>();
  MonsterTr = this.gameObject.GetComponent<Transform>();
  nv = GetComponent<NavMeshAgent>();
  nv.destination = PlayerTr.position;   //start to trace where player's position.
  anim = GetComponent<Animator>();
  hitID = Animator.StringToHash ("Base.gothit");   //char -> num..
  //01051464472
 }
 
 // Update is called once per frame
 void Update () {
  if(isdead) return;  //if monster is dead , exit Update()!!
  
  if(anim.IsInTransition (0) && anim.GetNextAnimatorStateInfo(0).nameHash == hitID)
  {
   nv.Resume();
   anim.SetBool("IsHit",false);
  }
  
  if(Vector3.Distance(MonsterTr.position,PlayerTr.position) < 2.0f)
  {
   nv.Stop();//stop trace~!!!!
   anim.SetBool("IsAttack",true);
   anim.SetBool("IsTrace",false);
  }else
  {
   if(Time.time > TraceTime)
   {
    nv.destination = PlayerTr.position;
    TraceTime = Time.time + 0.5f;
   }
   
   nv.Resume();   //restart to trace~!!!!
   anim.SetBool("IsAttack",false); 
   anim.SetBool("IsTrace",true);
   
  }
 }
 
 
 
 //catch conflict
 void OnCollisionEnter(Collision col)  //col is bullet
 {
  if(col.gameObject.tag == "Bullet" &&  !isdead)
  {
   nv.Stop();
   anim.SetBool("IsHit",true);
   
   StartCoroutine(this.CreateBlood(MonsterTr.position));   //make Blood effet
   StartCoroutine(this.CreateBloodPanel(MonsterTr.position));  //make Blood Panel
   
   hp -= col.gameObject.GetComponent<Bullet>().damage;    //how to use other's class variable.
   Destroy(col.gameObject);
   if(hp <= 0)
   {
          audio.PlayOneShot(killSound,1.0f);   //(sound name, volume)
           nv.Stop();
           anim.SetBool("IsDead",true);
    isdead = true;
    this.gameObject.GetComponent<CapsuleCollider>().enabled = false;
    
    Destroy(gameObject,3.0f);
   }
  }
  
 }//end of OnCollisionEnter
 
 
  IEnumerator CreateBlood(Vector3 pos)
  {
    GameObject newBlood = (GameObject)Instantiate(BloodEffect,pos,Quaternion.identity);
    Destroy(newBlood,0.3f);
  
    yield return null;
  }
 
 
  IEnumerator CreateBloodPanel(Vector3 pos)   // position value
  {
    Quaternion rotate = Quaternion.Euler(0,Random.Range(0,360),0); //valuable Blood effects   
    GameObject newBloodPanel = (GameObject)Instantiate(BloodPanel,pos,rotate);
    Destroy(newBloodPanel,3.0f);
  
    yield return null;
   
  }
  
  public void YouWin()
  {
   nv.Stop();
   isdead = true;
   anim.SetBool("IsGameOver",true);
  }

}

 

 

Button_1

 void OnGUI()
 {
    
      Rect rect2 = new Rect(Screen.width/2-50,Screen.height/2,100,25);
      if(GUI.Button(rect2,"처음으로"))
  {
       Application.LoadLevel("Title");
  }
  
      Rect rect3 = new Rect(Screen.width/2-50,Screen.height/2+30,100,25);
      if(GUI.Button(rect3,"게임종료"))
  {
        Application.Quit();
        Debug.Log("Quit");
  }
 }

 

Button_2

 public Texture2D nomalTexture;
 public Texture2D rollOverTexture;
 public AudioClip beep;
 public bool quitButton = false;

 void OnMouseExit()
 {
  guiTexture.texture = nomalTexture;
 }
 
 IEnumerator OnMouseUp()
 {
  audio.PlayOneShot(beep);
  yield return new WaitForSeconds(0.35f);
  if(quitButton)
  {
   Application.Quit();
   Debug.Log("quit");
  }else
  {
   Application.LoadLevel("Title");
  }
 }

 

 

 

 

 

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

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

 

충돌 감지후 스파크

 

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


PaingCube.vol1.egg


PaingCube.vol2.egg


PaingCube.vol3.egg


PaingCube.vol4.egg


'유니티3D > 예제' 카테고리의 다른 글

NGUI_강의  (0) 2013.12.05
PopUpButton.cs  (0) 2013.09.11
2DGameScripts  (0) 2013.09.10
Shoot  (0) 2013.09.03
UpDownRotate  (0) 2013.09.03


using UnityEngine;

using System.Collections;



[ExecuteInEditMode()]

public class PopUpButton : MonoBehaviour {

private Rect windowRect = new Rect(5, 5, 250, 100);

private bool visibleWindow = false;

// Use this for initialization

void OnGUI()

{

if(GUI.Button (new Rect(30,30,120,30), "<<")){

visibleWindow = true;

}

if(visibleWindow){

windowRect = GUI.Window(0, new Rect(Screen.width/2 - 150,Screen.height/2-100,300,180), DoMyWindow, "Painting Cube");

}

}

void DoMyWindow(int windowID){

//LabelText

//GUI.Label (new Rect(windowRect.width/2-120,25,240,30), "This is PopUp Window");

//Button

if(GUI.Button (new Rect(windowRect.width/2-100,windowRect.height*0.2f,200,30), "Restart"))

{

visibleWindow = false;

}

if(GUI.Button (new Rect(windowRect.width/2-100,windowRect.height*0.4f,200,30), "Quit"))

{

visibleWindow = false;

}

if(GUI.Button (new Rect(windowRect.width/2-100,windowRect.height*0.6f,200,30), "About Painting Cube"))

{

visibleWindow = false;

}

GUI.DragWindow(new Rect(0,0,10000,20));

}

}







'유니티3D > 예제' 카테고리의 다른 글

NGUI_강의  (0) 2013.12.05
페인팅큐브_프로토타입  (0) 2013.09.14
2DGameScripts  (0) 2013.09.10
Shoot  (0) 2013.09.03
UpDownRotate  (0) 2013.09.03

+ Recent posts