티스토리 뷰

 

1. 점수 표시하기 UI->Canvas

 

2. 점수 표시할 텍스트 넣기 UI->Text

Anchors는 Ctrl키, Shift키를 누르면 다양한 모드가 나온다. 

 

3. 점수 스크립트 생성하기 

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

public class Score : MonoBehaviour
{
    public static int nScore = 0;

    void Start()
    {
        nScore = 0; //새로 시작할때 점수를 0점으로 초기화
    }

    
    void Update()
    {
        GetComponent<Text>().text = nScore.ToString();
    }
}

 

4. 장애물 통과하면 점수 올려주게 만들기위해 장애물 사이에 BoxCollider2D 추가하기

 

5. 장애물 사이에 있는 BoxCollider2D를 통과하기 위해 IsTrigger를 체크한다. 

 

5. 장애물 통과시 스코어 올려주는 스크립트 만들기 

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

public class ObjstacleScore : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Score.nScore++; //장애물 통과시 점수를 1씩 올려준다.

    }
}

 

6. 게임오버씬 만들기 (씬 추가하기)

 

 

7. 게임오버씬에 UI -> 패널 -> 이미지 넣기

 

 

8. 패널에 게임오버이미지 넣기

 

9. 패널에 스코어텍스트 넣기

 

10. 패널에 다시시작하기 버튼 넣기 

 

10-1. SCORE TEXT에 스크립트 적용하기

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

public class currentScore : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GetComponent<Text>().text = "SCORE " + Score.nScore.ToString();   
    }

}

 

10-2. BESTSCORE TEXT 스크립트 적용하기

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

public class bestScore : MonoBehaviour
{
    void Start()
    {
        GetComponent<Text>().text = "BEST SCORE " + Score.nBestScore;
    }
   
}

 

11. 버드가 장애물과 충돌하면 게임오버씬으로가 가기 

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

public class JumpBird : MonoBehaviour
{

    Rigidbody2D rigidbody2D;
    public float jumpPower = 3.5f;
    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) //터치할때마다
        {
            rigidbody2D.velocity = Vector2.up * jumpPower; 
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        SceneManager.LoadScene("GameOverScene");
    }
}

 

12. Retry 버튼 누르면 다시 플레이하기 

 

13. BEST SCORE 갱신하기.

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

public class JumpBird : MonoBehaviour
{

    Rigidbody2D rigidbody2D;
    public float jumpPower = 3.5f;
    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) //터치할때마다
        {
            rigidbody2D.velocity = Vector2.up * jumpPower; 
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(Score.nScore > Score.nBestScore)
        {
            Score.nBestScore = Score.nScore; // 베스트 스코어 갱신
        }
        SceneManager.LoadScene("GameOverScene");
    }
}

 

 

 

댓글
최근에 달린 댓글
글 보관함
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글