9강 복습 유니티


===[ 이동설정 ]===========================================================

using UnityEngine;
using System.Collections;

public class player_move : MonoBehaviour{

    public float speed;

    void Start()  {   }
    void Update()   {

        // 키 입력 설정
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        // 키 입력했을때 방향 이동 설정
        gameObject.transform.Translate(Vector2.right * h * speed * Time.deltaTime);
        // 좌우 이동 제한 메소드 호출 
        AreaCheck();
    }

    void AreaCheck()
    {
        //Vector3 형 객체 변수 pos 생성
        Vector3 pos = transform.position;
        if (pos.x <= -2.5f) pos.x = -2.5f;
        if (pos.x >= 2.5f) pos.x = 2.5f;

        transform.position = pos;
    }


    // 운석 충돌시 Destroy(운석) col = 부딪히는 게임 오브젝트
    void OnTriggerEnter2D(Collider2D col)
    {
        Destroy(col.gameObject);
    }
}



===[ 코루틴 ]===========================================================
public class Meteor_pos : MonoBehaviour { public float minNum; public float maxNum;
    // 복제될 오브젝트 형 객체변수 생성
    public Object meteo;        

 void Start () {
        // 코루틴 실행
        StartCoroutine(MeteoGen());
 } 

 void Update () { }

    IEnumerator MeteoGen()
    {
        //무한반복.
        while (true)
        {
            // 랜덤시간 설정
            float ranTime = Random.Range(minNum, maxNum);
            
            //ranTime 이후에 하위 코드 실행
            yield return new WaitForSeconds(ranTime);
            
            //오브젝트 복제하여 scene 자신의 위치에서 생성
            Instantiate(meteo, transform.position, Quaternion.identity);
        }
    }
}

===[ 위치체크 후 삭세 ]=====================================================

public class Metro : MonoBehaviour
{
    int score;
    public float speed;
    public Text scoreText;
    GameObject text;
    
    void Start()
    {
        // text 형 게임오브젝트를 찾아 scoreText에 대입 GetComponent 반드시 사용.
        scoreText = GameObject.Find("scText").GetComponent<Text>();
    }
        
    void Update()
    {
        transform.Translate(-Vector2.up * speed * Time.deltaTime);
        CheckArea();
    }
    // 위치 체크후 게임 오브젝트 삭제
    void CheckArea()
    {
        Vector3 pos = transform.position;
        if (pos.y <= -4.5)
        {
            score++;
            scoreText.text = score.ToString();
            Destroy(gameObject);
        }
    }
}


















댓글

이 블로그의 인기 게시물

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

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

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