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


Roll a Ball 게임 Pick Up Objects 수집



PlayerController에 코드를 추가해 줍니다.

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
using UnityEngine;
using System.Collections;
 
public class PlayerController : MonoBehaviour
{
 
    public float speed;
 
    private Rigidbody rb;
 
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
 
    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);
        }
    }
}
cs



Pick Up의 태그를 지정해 줍시다.




정확하게 같은 이름을 사용해야 하므로 복붙을 추천 합니다.






Pick Up을 선택


Is Trigger 체크




게임 실행 할시 상자들이  중력에 의해 바닥을 통과 하면서 떨어진다.

리지드바디를 이용해 다음과 같은 상황을 해결한다.





1. 중력을 없애는 방법



2.Is Kinematic을 사용하는 방법(추천)

물리력에 반응하지 않고 트랜스 폼을 이욯.



 이제 상자가 떨어지지 않고 완젹하게 움직입니다. 

Roll a Ball 게임 Pick Up Objects








Pick Up  이라는 오브젝트를 만들어준후 리셋





상자 만들어 주기




Pick Up을 다음과 같이 드래그 해주세요



이렇게 쏙 들어 온답니다.



Rotator  스크립트를 만들어 주어 회전하는 상자를 만들어 보겠습니다 


아래와 같이 코드 작성

1
2
3
4
5
6
7
8
9
10
11
using UnityEngine;
using System.Collections;
 
public class Rotator : MonoBehaviour
{
 
    void Update()
    {
        transform.Rotate(new Vector3(153045* Time.deltaTime);
    }
}
cs




Pick Up의 부모 오브젝트 Pickups를 만들어 줍니다. 리셋 필수



방금 만든 Pick Up오브젝트를  ctrl + D하면 복사가 됩니다 

12 개를 만들어 준후 손으로 위치를 지정해 주십시요 



기존에 만들 었던 Background를 Ctrl + D를 하여 Pick Up 을 만들어 줍니다.

색상 지정


전체 상자를 노란색으로 만드는 방법 

픽업을 다음과 같이 드래그 해주세요






+ Recent posts