10강 슈팅게임 만들기 - 복습


===[ PLAYER ]============================================================

using UnityEngine;
using System.Collections;
// ui 를 사용할때 반드시 사용 해줘야 함.
using UnityEngine.UI;

// 스피드 변수 생성
// 이동값 입력받음
// 이동값 대입
// 이동제한 
public class CPlayer : MonoBehaviour
{

    public int _hp = 100;
    public int _score;
    
    //스코어
    public Text scoreText;
    //이미지바
    public Image hpBar;

    //폭파 이펙트
    public Object Explo_effect;

    public float speed = 5;
    Vector2 pos;

    // Use this for initialization
    void Start()
    {

    }

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

        float h = Input.GetAxis("Horizontal");
        // 이동 메소드
        transform.Translate(Vector2.right * h * speed * Time.deltaTime);
        // 이동제한 메소드 실행
        CheckArea();


    }
    // 이동제한 메소드 2.8
    void CheckArea()
    {
        pos = transform.position;

        if (pos.x <= -2.8f) pos.x = -2.8f;
        if (pos.x >= 2.8f) pos.x = 2.8f;

        transform.position = pos;

    }


    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "metero" || col.tag == "item")
        {
            Destroy(col.gameObject);

            // explo 프리팹 생성
            // col.transform.position = 부딪힌 충돌체의 위치에서 생성
            // Instantiate는 게임 오브젝트를 반환 해 준다.    
            if (col.tag == "metero")
            {
                // hp감소
                _hp -= 10;
                //Hp 바 감소
                if (_hp == 0)
                {
                    Application.LoadLevel("end");
                }

                hpBar.fillAmount = _hp * 0.01F; 
                
                GameObject Smoke =
                Instantiate(Explo_effect, col.transform.position, Quaternion.identity)
                as GameObject;
                // 0.8초 후에 explo 프리팹 제거
                Destroy(Smoke, 0.8f);
            }
            // 아이템 획득시
            if (col.tag == "item")
            {
                _score += 10;
                scoreText.text = _score.ToString();
                
                /*
                _score = int.Parse(scoreText.text);
                _score += 10;
                scoreText.text = _score.ToString();
                 */
            }

        }
    }

}


===[ MeteroMove ]=======================================================
using UnityEngine; using System.Collections; using UnityEngine.UI; public class CMteroMove : MonoBehaviour { public float speed = 5; Vector2 pos; public int score; void Start() { } void Update() { // 운석 이동 메소드 transform.Translate(-Vector2.up * speed * Time.deltaTime); //영역 제한 메소드 호출 CheckArea(); } // 영역 제한 메소드 -5.2 void CheckArea() { pos = transform.position; if (pos.y <= -5.2) { Text scoreTxt = GameObject.Find("scoreText").GetComponent<Text>(); score = int.Parse(scoreTxt.text); score += 1; scoreTxt.text = score.ToString(); Destroy(gameObject); } } }
===[ MeteroGen ]=======================================================
using UnityEngine; using System.Collections; public class CEnemy : MonoBehaviour { // 생성될 게임 오브젝트 public Object[] objects; // mintime public float minTime; // maxtime public float maxTime; void Start() { // 반드시 기억할것!!!! StartCoroutine(GenTimer()); } void Update() { } public IEnumerator GenTimer() { while (true) { //오브젝트 랜덤 확률 int ranNum = Random.Range(0, 10); //확율 60% 0,1,2,3,4,5 if (ranNum < 6 && ranNum >= 0) ranNum = 0; //확율 30% 6,7,8 else if (ranNum < 9 && ranNum >= 6) ranNum = 1; // 확율 10% 9 else ranNum = 2; // 랜덤 타임 float randTime = Random.Range(minTime, maxTime); // randTime(초)후에 다음 코드 실행 yield return new WaitForSeconds(randTime); Instantiate(objects[ranNum], transform.position, Quaternion.identity); } } }

댓글

이 블로그의 인기 게시물

날짜 시간 시간차 시간 계산 하기

코루틴에서 CallBack 함수 적용하기

C++ 언어 퍼센트 구하는 방법 / 기본 언어 퍼센트 구하는 방법