Roll a Ball 게임 Pick Up Objects 마무리


UNITY 도구를 이용해 UI텍스트 요소를 생성

점수 계산, 텍스트 표시 및 게임 종료하기.


수집 가능한 오브젝트의 계산 값을 저장할 수 있는 도구 필요.

그 값을 수집 및 계산 하면서 그 값을 더할 또 다른 도구도 필요합니다.

이 도구를 Player Controller 스크립트에 추가 합시다.




Canvas가 무조건 부모가 되어야 한다.





다음과 같이 설정


A|t + Shift 누르고 다음과 같이 클릭 하면 


이렇게 됩니다.


좀더 보기 좋게 만들겠습니다.

짜짠




스크립트에서 코드를 작성하고 (코드는 아래 첨부)

드래그해서 넣습니다,


스코어가 올라 갑니다.



위와 같은 방법으로 Win Text를 만들어 줍니다.



다시 드래그해서 넣어 줍시다.


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
 
public class PlayerController : MonoBehaviour
{
 
    public float speed;
    public Text countText;
    public Text winText;
 
    private Rigidbody rb;
 
    private int count; //점수를 수용할 변수
 
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        SetCountText();
        winText.text = "";
    }
 
    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
 
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
        rb.AddForce(movement * speed);
    }
 
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Pick Up"))
        {
            other.gameObject.SetActive(false);
            count = count + 1;
            SetCountText();
        }
    }
 
    void SetCountText()
    {
        countText.text = "Count: " + count.ToString();
        if (count >=12)
        {
            winText.text= "You Win!";
        }
    }
}
cs


+ Recent posts