Sokovan 게임제작 코드

Se0ng_1l·2022년 7월 22일
0
post-thumbnail

Player.cs

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

public class Player : MonoBehaviour
{
    public GameManager gameManager;
    public float speed = 10f; // 속도지정
    private Rigidbody playerRigidbody; // 리지드바디를 통해 속도와 힘을 가한다.
    void Start()
    {
        playerRigidbody = GetComponent<Rigidbody>();
    }

    void Update()
    {
        if (!gameManager.isGameOver)
        {
            // 오른쪽키 왼쪽키 입력시 -1 ~ +1을 리턴
            float inputX = Input.GetAxis("Horizontal");
            float inputZ = Input.GetAxis("Vertical");
            float fallSpeed = playerRigidbody.velocity.y;
            Vector3 velocity = new Vector3(inputX, 0, inputZ);
        
            velocity *= speed; // (inputX * speed, 0, inputZ * speed)
            velocity.y = fallSpeed; // (inputX * speed, fallSpeed, inputZ * speed)

            playerRigidbody.velocity = velocity;
        }
    }
}

Rotater.cs

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

public class Rotater : MonoBehaviour
{
    void Update()
    {
        transform.Rotate(60 * Time.deltaTime, 60 * Time.deltaTime, 60 * Time.deltaTime);
    }
}

ItemBox.cs

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

public class ItemBox : MonoBehaviour
{
    Renderer myRenderer;
    public bool isOverrapped = false;
    
    public Color touchColor;
    Color originalColor;
    void Start()
    {
        myRenderer = GetComponent<Renderer>();
        originalColor = myRenderer.material.color;
    }
    
    // Enter = 충돌 한 순간
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "EndPoint")
        {
            isOverrapped = true;
            myRenderer.material.color = touchColor;
        }
    }

    // 충돌 벗어날 때
    void OnTriggerExit(Collider other)
    {
        if (other.tag == "EndPoint")
        {
            isOverrapped = false;
            myRenderer.material.color = originalColor;
        }
    }
    // 충돌하고 있는 공안
    void OnTriggerStay(Collider other)
    {
        if (other.tag == "EndPoint")
        {
            isOverrapped = true;
            myRenderer.material.color = touchColor;
        }
    }
    
}

GameManager.cs

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

public class GameManager : MonoBehaviour
{
    public ItemBox[] itemBoxes;
    public bool isGameOver;
    public GameObject winUI;
    void Start()
    {
        isGameOver = false;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SceneManager.LoadScene(0);
        }
        if (isGameOver == true)
            return;
        int cnt = 0;
        for (int i = 0; i < 3; i++)
        {
            if (itemBoxes[i].isOverrapped == true)
            {
                cnt++;
            }
        }

        if (cnt >= 3)
        {
            isGameOver = true;
            winUI.SetActive(true);
            Debug.Log("게임 승리");
        }
    }
}
profile
치타가 되고 싶은 취준생

0개의 댓글