사전캠프 (빗물 받는 르탄이)

상원·2025년 1월 15일
0

마우스 좌클릭을 누르면 르탄이가 방향 전환을 하고 계속해서 떨어지는 빗방울을 받아
점수를 얻는 게임입니다.
시간제한은 30초이고 30초가 지나면 다시 플레이 할 수있는 버튼이 팝업되며, 버튼을 누르면
게임을 다시 시작하게 됩니다.

//Rtan 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rtan : MonoBehaviour
{
    float direction = 0.05f;

    SpriteRenderer renderer;

    // Start is called before the first frame update
    void Start()
    {
        Application.targetFrameRate = 60;
        renderer = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            direction *= -1;
            renderer.flipX = !renderer.flipX;
        }

        if (transform.position.x > 2.6f)
        {
            renderer.flipX = true;
            direction = -0.05f;
        }

        if(transform.position.x < -2.6f)
        {
            renderer.flipX = false;
            direction = 0.05f;
        }

        transform.position += Vector3.right * direction;
    }
}

Start함수는 한번만 호출되므로 끊임없이 움직여야하는 연산은 Update함수에 구현
Application.targetFrameRate 변수를 이용해 이동거리가 모든 환경에서 같도록 조정
Input 클래스를 통해 마우스 클릭 입력을 가져왔습니다.

//Rain 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rain : MonoBehaviour
{
    float size = 1.0f;
    int score = 1;

    SpriteRenderer renderer;

    // Start is called before the first frame update
    void Start()
    {
        renderer = GetComponent<SpriteRenderer>();

        float x = Random.Range(-2.4f, 2.4f);
        float y = Random.Range(3.0f, 5.0f);

        transform.position = new Vector3(x, y, 0);

        int type = Random.Range(1,4);

        if (type == 1)
        {
            size = 0.8f;
            score = 1;
            renderer.color = new Color(100 / 255f, 100 / 255f, 1f, 1f);
        }
        else if (type == 2) 
        {
            size = 1.0f;
            score = 2;
            renderer.color = new Color(130 / 255f, 130 / 255f, 1f, 1f);
        }
        else if (type == 3)
        {
            size = 1.2f;
            score = 3;
            renderer.color = new Color(150 / 255f, 150 / 255f, 1f, 1f);
        }  

        transform.localScale = new Vector3 (size, size, 0);
    }


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

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            Destroy(this.gameObject);
        }
        else if (collision.gameObject.CompareTag("Player"))
        {
            GameManager.Instance.AddScore(score);
            Destroy(this.gameObject);
        }
    }
}

빗물에 Rigidbody2D컴포넌트를 추가하여 물리작용(중력)을 적용
함수를 이용해 Ground,Player태그와 부딪혔을때 빗물을 상호작용

//GameManager 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;
    public GameObject rain;
    public GameObject endPanel;
    public Text totalScoreTxt;
    public Text timeTxt;

    int totalScore;

    float totalTime = 30.0f;

    private void Awake()
    {
        Instance = this;
        Time.timeScale = 1.0f;
    }

    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("MakeRain",0f,1f);
    }

    // Update is called once per frame
    void Update()
    {
        if (totalTime > 0f)
        {
            totalTime -= Time.deltaTime;
        }
        else
        {
            totalTime = 0f;
            Time.timeScale = 0f;
            endPanel.SetActive(true);
        }
        timeTxt.text = totalTime.ToString("N2");
    }

    void MakeRain()
    {
        Instantiate(rain);
    }

    public void AddScore(int score)
    {
        totalScore += score;
        totalScoreTxt.text = totalScore.ToString();
    }
}

GameManager 클래스를 이용하여 플레이 시간과 빗방울을 계속 생성하고 점수를 더하며 플레이 시간이 끝난다면 종료하였습니다.

//RetryButton 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class RetryButton : MonoBehaviour
{
   public void Retry()
    {
        SceneManager.LoadScene("MainScene");
    }

}

게임이 끝나고 Retry버튼이 노출되면 버튼을 통해 MainScene을 불러오게 됩니다.

0개의 댓글