简体   繁体   中英

C# Unity Puzzle Game

I want to create a game where all tiles must be rotated to the correct position. I have 15 tiles with a script attached which rotates them 90 degrees on each click. I can detect when each tile is in the correct position but what I am having trouble doing is detecting when ALL 15 are in the correct position.

Here is a screen shot of my stage

https://www.dropbox.com/s/9ag4ly2bil1mzf1/Untitled-1.jpg?dl=0

I was thinking I can create an empty game object and attach a new script with code to get the value from each tile and check if all are true (in the correct position).

Here is my basic code for the rotation script

using UnityEngine;
using System.Collections;

public class rotate : MonoBehaviour {

    public float rotationAnswer;
    // Use this for initialization
    void Start() {}

    // Update is called once per frame
    void Update() {}

    float rotationValue = 0;

    void OnMouseDown(){
        transform.Rotate(Vector3.up * 90);
        rotationValue = transform.rotation.eulerAngles.y;
        Debug.Log ( rotationValue );

        if ( rotationValue == rotationAnswer)
        {
            Debug.Log ( "Correct" );
            switch (this.name )
            {
            case "tile1":

            }

            Debug.Log ( GameObject.Find("GameComplete").GetComponent<TileGameComplete>().totalCorrectTiles ))

        } else {
        Debug.Log ( "Wrong" );
        }
    }
}

Here is my basic code for the detection and complete game script

using UnityEngine;
using System.Collections;

public class TileGameComplete : MonoBehaviour {

    public int currentCorrectTiles = 0;
    public bool tile1 = false;
    public bool tile2 = false;
    public int totalCorrectTiles = 15;
    // Use this for initialization

    void Update() 
    {
        GameObject.Find("Tile1").GetComponent<rotate>().done
        if (tile1 && tile2) 
        {
            Debug.Log ("You've won");
        }
    }
}

How would you go about solving this problem in C#. Thanks :)

Keep a shared or static variable that gets set to zero at the start of the level. When a shape is moved into the correct rotation add one to the count, and if a shape is moved out of correct rotation subtract one from the count. When the count is equal to 15 the puzzle is solved.

我正在工作,目前无法访问Unity,因此无法给您代码,但是如果您将场景中另一个(空)GameObject的所有15个tile子对象全部设为孩子,则可以访问该父对象的子对象集合,进行遍历以检查它们是否都已正确旋转。

@Agumander has a good answer, but personally I prefer to keep logic simple. I would recommend creating a GameController that has access to all 15 tiles, either via a public static list or GameObject.find(...) That way you won't have to check the list 15 times each time the game update for each tile. The tile just has to do their own job (Rotate) And the game controller will determine the logic to end/restart the game

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM