본문 바로가기
대외 활동/유니티 스터디

[unity] class1_오브젝트배치, 움직임

by ballbig 2021. 7. 22.
728x90

 

결과물

 


추가로 실행해본 것_1

바늘이 움직이는 룰렛 게임

 

1. Roulette이 아닌, 가운데에 needle이 돌아가도록 하여 룰렛 게임을 구현해 주었다.

 

2. 화면을 클릭하는 길이에 비례하여 룰렛이 돌아가는 시간도 길어지도록 구현해 주었다.

 

전체 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cameraControl : MonoBehaviour
{
    float rotSpeed = 0;
    int onf = 0;
    float angle = 0;
    void Start()
    {

    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            onf = 1;
        }

        if (onf == 1) //마우스 클릭시 아래 코드 실행
        {
            if (Input.GetMouseButton(0)) //마우스를 클릭하고 있다면
            {//마우스 클릭시 하고 있는 시간 만큼 회전 각도 늘림
                this.rotSpeed += 0.25f;
                Debug.Log(this.rotSpeed);
            }
            else //마우스를 클릭을 땠을 때 회전 시작
            {
                this.rotSpeed *= 0.96f;
                transform.Rotate(0, 0, this.rotSpeed);
                Debug.Log(this.rotSpeed);
            }

            if (this.rotSpeed < 0.01) //회전 멈출 시 초기화
            {
                onf = 0;
            }
        }
    }
}

 

 

추가로 실행해본 것_2

 

1. MainCamera를 움직여 첫 시작 시 Roulette이 서서히 나타나도록 효과를 주었다.

 

2. 뒷 배경이 허전한 듯 싶어 transform.Translate()함수를 이용해 배경에 간식들이 움직이도록 꾸며주었다.

 

3. Roulette을 돌려서 나온 간식이 Console창에서도 나오게 해주었다.

 

전체코드

[main camera에 적용된 코드]

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

public class start : MonoBehaviour
{

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (this.transform.position.x > 0)
        // mainCamera가 룰렛중앙으로 올때까지 x방향으로 움직임
        {
            this.transform.Translate(-0.05f, 0f, 0f);
        }
    }
}

 

 

[뒷 배경 간식에 적용된 코드]

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


public class moving : MonoBehaviour
{
    float speed;
    void Start()
    {
        this.speed = -0.01f;
    }

    void Update()
    {
        if (this.transform.position.y < -6.5) // 화면 밖으로 나가면
        {
            this.transform.Translate(0, 13, 0f); // 처음 위치로 다시 이동시킴
        }
        else
        {
            this.transform.Translate(0f, speed, 0f); // y방향 이동
        }
    }
}

 

 

[룰렛에 적용된 코드]

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

public class Roulette_Controller : MonoBehaviour
{
    float rotSpeed = 0;
    int onf = 0;
    float angle = 0;
    void Start() { }

    void Update()
    {
        if (Input.GetMouseButtonDown(0)) //마우스를 클릭하면
        {
            onf = 1; // 시작상태 on 시킴
        }

        if (onf == 1) // 시작상태가 on되고
        {
            if (Input.GetMouseButton(0)) //마우스를 클릭하고 있다면
            {
                this.rotSpeed += 1; // 클릭하고 있는 시간 만큼 회전각도를 늘림
            }
            else // 마우스 클릭을 뗏다면 rotSpeed만큼 룰렛을 돌리기 시작함
            {
                this.rotSpeed *= 0.96f;
                transform.Rotate(0, 0, this.rotSpeed);
            }

            if (this.rotSpeed < 0.01) //룰렛 회전이 멈췄을 때
            {
                onf = 0;
                angle = this.transform.eulerAngles.z / 60;
                //현재 선택된 간식에 해당하는 각도는 angle이라는 변수에 할당
                if (0 <= angle & angle < 1) //각도가 0~60도 사이 =  고구마
                {
                    Debug.Log("고구마");
                }
                if (1 <= angle & angle < 2) //각도가 60~120도 사이=아이스크림
                {
                    Debug.Log("아이스크림");
                }
                if (2 <= angle & angle < 3) //각도가 120~180도 사이 = 핫도그
                {
                    Debug.Log("핫도그");
                }
                if (3 <= angle & angle < 4) //각도가 180~240도 사이 = 케이크
                {
                    Debug.Log("케이크");
                }
                if (4 <= angle & angle < 5) //각도가 240~300도 사이 = 붕어빵
                {
                    Debug.Log("붕어빵");
                }
                if (5 <= angle & angle < 6) //각도가 300~360도 사이 = 떡꼬치
                {
                    Debug.Log("떡꼬치");
                }
            }
        }
    }
}