네비게이션, 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); }
} |
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"); } } |