Unity做一个魔方

最近项目周期变得平稳,不再像之前那么忙了,所以抽空写了个魔方小游戏,在这里跟大家分享。

游戏原理并不复杂,在场景中,我们放了27个Cube,和6个面的触发器,根据触发器的Enter检测和Exit检测,确定对应面的子物体,使得触发器底下的9个Cube能同时转动,同时,再设置X,Y,Z三个轴向的整体转动,子物体是固定的27个Cube。

有了思路以后,我们来做场景,做完场景以后,画面大概是这样:

在FrontContent及以下5个物体中,我们要挂靠BoxCollider,和Rigibody组件,如图:

接下来,我们来写脚本,因为总体逻辑并不复杂,所以主逻辑只写了了一个GameController脚本,另外还有6个触发器脚本,以下是代码:

GameController.cs

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

public class GameController : MonoBehaviour {

    public static GameController Instance;

    public enum Axis {//旋转轴
        X = 1,
        Y = 2,
        Z = 3
    }

    public enum RotateModel {//旋转模式
        Forward = 90,
        Back = -90
    }

    public enum Direction {//旋转方位
        First = -90,
        Second = -180,
        Third = 90,
        Fourth = 0
    }

    private Direction cubeDirF;//F面转向
    private Direction cubeDirB;//B面转向
    private Direction cubeDirU;//U面转向
    private Direction cubeDirD;//D面转向
    private Direction cubeDirL;//L面转向
    private Direction cubeDirR;//R面转向
    private Direction cubeDirWholeX;//整体转向
    private Direction cubeDirWholeY;
    private Direction cubeDirWholeZ;

    private const float rotateSpeed = 150f;//旋转速度

    private Quaternion targetRotationF;//旋转角度
    private Quaternion targetRotationB;
    private Quaternion targetRotationU;
    private Quaternion targetRotationD;
    private Quaternion targetRotationL;
    private Quaternion targetRotationR;
    private Quaternion targetRotationWholeX;
    private Quaternion targetRotationWholeY;
    private Quaternion targetRotationWholeZ;

    private int targetFZ = 0;//旋转轴度数
    private int targetBZ = 0;
    private int targetUY = 0;
    private int targetDY = 0;
    private int targetLX = 0;
    private int targetRX = 0;
    private int targetWholeX = 0;
    private int targetWholeY = 0;
    private int targetWholeZ = 0;

    public Button FButton;
    public Button FAntiButton;
    public Button BButton;
    public Button BAntiButton;
    public Button UButton;
    public Button UAntiButton;
    public Button DButton;
    public Button DAntiButton;
    public Button LButton;
    public Button LAntiButton;
    public Button RButton;
    public Button RAntiButton;
    public Button XButton;
    public Button XAntiButton;
    public Button YButton;
    public Button YAntiButton;
    public Button ZButton;
    public Button ZAntiButton;

    public GameObject FrontContent;//旋转父物体
    public GameObject BackContent;
    public GameObject UpContent;
    public GameObject DownContent;
    public GameObject LeftContent;
    public GameObject RightContent;
    public GameObject WholeXContent;
    public GameObject WholeYContent;
    public GameObject WholeZContent;
    private GameObject prevContent;//上一个转动的模块

    public List<GameObject> FrontObjList;//旋转面方块集合
    public List<GameObject> BackObjList;
    public List<GameObject> UpObjList;
    public List<GameObject> DownObjList;
    public List<GameObject> LeftObjList;
    public List<GameObject> RightObjList;
    public List<GameObject> WholeObjList; 

    private bool isFrontClockwise = false;//旋转判断条件
    private bool isFrontAntiClockwise = false;
    private bool isBackClockwise = false;
    private bool isBackAntiClockwise = false;
    private bool isUpClockwise = false;
    private bool isUpAntiClockwise = false;
    private bool isDownClockwise = false;
    private bool isDownAntiClockwise = false;
    private bool isLeftClockwise = false;
    private bool isLeftAntiClockwise = false;
    private bool isRightClockwise = false;
    private bool isRightAntiClockwise = false;
    private bool isXClockwise = false;
    private bool isXAntiClockwise = false;
    private bool isYClockwise = false;
    private bool isYAntiClockwise = false;
    private bool isZClockwise = false;
    private bool isZAntiClockwise = false;
    private bool isPlaying = false;//正在旋转

    void Awake()
    {
        Instance = this;
    }

    void Start () {
        FButton.onClick.AddListener(OnClickFBtn);
        FAntiButton.onClick.AddListener(OnClickFAntiBtn);
        BButton.onClick.AddListener(OnClickBBtn);
        BAntiButton.onClick.AddListener(OnClickBAntiBtn);
        UButton.onClick.AddListener(OnClickUBtn);
        UAntiButton.onClick.AddListener(OnClickUAntiBtn);
        DButton.onClick.AddListener(OnClickDBtn);
        DAntiButton.onClick.AddListener(OnClickDAntiBtn);
        LButton.onClick.AddListener(OnClickLBtn);
        LAntiButton.onClick.AddListener(OnClickLAntiBtn);
        RButton.onClick.AddListener(OnClickRBtn);
        RAntiButton.onClick.AddListener(OnClickRAntiBtn);

        XButton.onClick.AddListener(OnClickXBtn);
        XAntiButton.onClick.AddListener(OnClickXAntiBtn);
        YButton.onClick.AddListener(OnClickYBtn);
        YAntiButton.onClick.AddListener(OnClickYAntiBtn);
        ZButton.onClick.AddListener(OnClickZBtn);
        ZAntiButton.onClick.AddListener(OnClickZAntiBtn);
    }

    void Update () {
        UpdateFrontClockwise();
        UpdateFrontAntiClockwise();
        UpdateBackClockwise();
        UpdateBackAntiClosewise();
        UpdateUpClockwise();
        UpdateUpAntiClockwise();
        UpdateDownClockwise();
        UpdateDownAntiClockwise();
        UpdateLeftClockwise();
        UpdateLeftAntiClockwise();
        UpdateRightClockwise();
        UpdateRightAntiClockwise();

        UpdateXAxisClockwise();
        UpdateXAxisAntiClockwise();
        UpdateYAxisClockwise();
        UpdateYAxisAntiClockwise();
        UpdateZAxisClockwise();
        UpdateZAxisAntiClockwise();
    }

    public void OnClickFBtn()//点击F按钮
    {
        ClickBtnEvent(RotateModel.Back, Axis.Z, ref FrontContent, ref FrontObjList, ref targetFZ, ref cubeDirF, ref targetRotationF, ref isFrontClockwise);
    }

    public void OnClickFAntiBtn()//点击F'按钮
    {
        ClickBtnEvent(RotateModel.Forward, Axis.Z, ref FrontContent, ref FrontObjList, ref targetFZ, ref cubeDirF, ref targetRotationF, ref isFrontAntiClockwise);
    }

    private void UpdateFrontClockwise()//F面正向旋转
    {
        UpdateRotateZEvent(Vector3.back, cubeDirF, targetRotationF, RotateModel.Back, ref FrontContent, ref isFrontClockwise, ref isFrontAntiClockwise);
    }

    private void UpdateFrontAntiClockwise()//F面反向旋转
    {
        UpdateRotateZEvent(Vector3.forward, cubeDirF, targetRotationF, RotateModel.Forward, ref FrontContent, ref isFrontAntiClockwise, ref isFrontClockwise);
    }

    public void OnClickBBtn()//点击B按钮
    {
        ClickBtnEvent(RotateModel.Forward, Axis.Z, ref BackContent, ref BackObjList,ref targetBZ, ref cubeDirB, ref targetRotationB, ref isBackClockwise);
    }

    public void OnClickBAntiBtn()//点击B'按钮
    {
        ClickBtnEvent(RotateModel.Back, Axis.Z, ref BackContent, ref BackObjList, ref targetBZ, ref cubeDirB, ref targetRotationB, ref isBackAntiClockwise);
    }

    public void UpdateBackClockwise()//B面正向旋转
    {
        UpdateRotateZEvent(Vector3.forward, cubeDirB, targetRotationB, RotateModel.Forward, ref BackContent,ref isBackClockwise,ref isBackAntiClockwise);
    }

    public void UpdateBackAntiClosewise()//B面反向旋转
    {
        UpdateRotateZEvent(Vector3.back, cubeDirB, targetRotationB, RotateModel.Back, ref BackContent, ref isBackAntiClockwise, ref isBackClockwise);
    }

    public void OnClickUBtn()//点击U按钮
    {
        ClickBtnEvent(RotateModel.Forward, Axis.Y, ref UpContent, ref UpObjList, ref targetUY, ref cubeDirU, ref targetRotationU, ref isUpClockwise);
    }

    public void OnClickUAntiBtn()//点击U'按钮
    {
        ClickBtnEvent(RotateModel.Back, Axis.Y, ref UpContent, ref UpObjList, ref targetUY, ref cubeDirU, ref targetRotationU, ref isUpAntiClockwise);
    }

    public void UpdateUpClockwise()//U面正向旋转
    {
        UpdateRotateYEvent(Vector3.up, cubeDirU, targetRotationU, RotateModel.Forward, ref UpContent, ref isUpClockwise, ref isUpAntiClockwise);
    }

    public void UpdateUpAntiClockwise()//U面反向旋转
    {
        UpdateRotateYEvent(Vector3.down, cubeDirU, targetRotationU, RotateModel.Back, ref UpContent, ref isUpAntiClockwise, ref isUpClockwise);
    }

    public void OnClickDBtn()//点击D按钮
    {
        ClickBtnEvent(RotateModel.Back, Axis.Y, ref DownContent, ref DownObjList, ref targetDY, ref cubeDirD, ref targetRotationD, ref isDownClockwise);
    }

    public void OnClickDAntiBtn()//点击D'按钮
    {
        ClickBtnEvent(RotateModel.Forward, Axis.Y, ref DownContent, ref DownObjList, ref targetDY, ref cubeDirD, ref targetRotationD, ref isDownAntiClockwise);
    }

    public void UpdateDownClockwise()//D面正向旋转
    {
        UpdateRotateYEvent(Vector3.down, cubeDirD, targetRotationD, RotateModel.Back, ref DownContent, ref isDownClockwise, ref isDownAntiClockwise);
    }

    public void UpdateDownAntiClockwise()//D面反向旋转
    {
        UpdateRotateYEvent(Vector3.up, cubeDirD, targetRotationD, RotateModel.Forward, ref DownContent, ref isDownAntiClockwise, ref isDownClockwise);
    }

    public void OnClickLBtn()//点击L按钮
    {
        ClickBtnEvent(RotateModel.Back, Axis.X, ref LeftContent, ref LeftObjList, ref targetLX, ref cubeDirL, ref targetRotationL, ref isLeftClockwise);
    }

    public void OnClickLAntiBtn()//点击L'按钮
    {
        ClickBtnEvent(RotateModel.Forward, Axis.X, ref LeftContent, ref LeftObjList, ref targetLX, ref cubeDirL, ref targetRotationL, ref isLeftAntiClockwise);
    }

    public void UpdateLeftClockwise()//L面正向旋转
    {
        UpdateRotateXEvent(Vector3.left, cubeDirL, targetRotationL, RotateModel.Back, ref LeftContent, ref isLeftClockwise, ref isLeftAntiClockwise);
    }

    public void UpdateLeftAntiClockwise()//L面反向旋转
    {
        UpdateRotateXEvent(Vector3.right, cubeDirL, targetRotationL, RotateModel.Forward, ref LeftContent, ref isLeftAntiClockwise, ref isLeftClockwise);
    }

    public void OnClickRBtn()//点击R按钮
    {
        ClickBtnEvent(RotateModel.Forward, Axis.X, ref RightContent, ref RightObjList, ref targetRX, ref cubeDirR, ref targetRotationR, ref isRightClockwise);
    }

    public void OnClickRAntiBtn()//点击R'按钮
    {
        ClickBtnEvent(RotateModel.Back, Axis.X, ref RightContent, ref RightObjList, ref targetRX, ref cubeDirR, ref targetRotationR, ref isRightAntiClockwise);
    }

    public void UpdateRightClockwise()//R面正向旋转
    {
        UpdateRotateXEvent(Vector3.right, cubeDirR, targetRotationR, RotateModel.Forward, ref RightContent, ref isRightClockwise, ref isRightAntiClockwise);
    }

    public void UpdateRightAntiClockwise()//R面反向旋转
    {
        UpdateRotateXEvent(Vector3.left, cubeDirR, targetRotationR, RotateModel.Back, ref RightContent, ref isRightAntiClockwise, ref isRightClockwise);
    }

    public void OnClickXBtn()//点击X按钮
    {
        ClickBtnEvent(RotateModel.Forward, Axis.X, ref WholeXContent, ref WholeObjList, ref targetWholeX, ref cubeDirWholeX, ref targetRotationWholeX, ref isXClockwise);
    }

    public void OnClickXAntiBtn()//点击X'按钮
    {
        ClickBtnEvent(RotateModel.Back, Axis.X, ref WholeXContent, ref WholeObjList, ref targetWholeX, ref cubeDirWholeX, ref targetRotationWholeX, ref isXAntiClockwise);
    }

    public void UpdateXAxisClockwise()//X轴正向旋转
    {
        UpdateRotateXEvent(Vector3.right, cubeDirWholeX, targetRotationWholeX, RotateModel.Forward, ref WholeXContent, ref isXClockwise, ref isXAntiClockwise);
    }

    public void UpdateXAxisAntiClockwise()//X轴反向旋转
    {
        UpdateRotateXEvent(Vector3.left, cubeDirWholeX, targetRotationWholeX, RotateModel.Back, ref WholeXContent, ref isXAntiClockwise, ref isXClockwise);
    }

    public void OnClickYBtn()//点击Y按钮
    {
        ClickBtnEvent(RotateModel.Forward, Axis.Y, ref WholeYContent, ref WholeObjList, ref targetWholeY, ref cubeDirWholeY, ref targetRotationWholeY, ref isYClockwise);
    }

    public void OnClickYAntiBtn()//点击Y'按钮
    {
        ClickBtnEvent(RotateModel.Back, Axis.Y, ref WholeYContent, ref WholeObjList, ref targetWholeY, ref cubeDirWholeY, ref targetRotationWholeY, ref isYAntiClockwise);
    }

    public void UpdateYAxisClockwise()//Y轴正向旋转
    {
        UpdateRotateYEvent(Vector3.up, cubeDirWholeY, targetRotationWholeY, RotateModel.Forward, ref WholeYContent, ref isYClockwise, ref isYAntiClockwise);
    }

    public void UpdateYAxisAntiClockwise()//Y轴反向旋转
    {
        UpdateRotateYEvent(Vector3.down, cubeDirWholeY, targetRotationWholeY, RotateModel.Back, ref WholeYContent, ref isYAntiClockwise, ref isYClockwise);
    }
    public void OnClickZBtn()//点击Z按钮
    {
        ClickBtnEvent(RotateModel.Forward, Axis.Z, ref WholeZContent, ref WholeObjList, ref targetWholeZ, ref cubeDirWholeZ, ref targetRotationWholeZ, ref isZClockwise);
    }

    public void OnClickZAntiBtn()//点击Z'按钮
    {
        ClickBtnEvent(RotateModel.Back, Axis.Z, ref WholeZContent, ref WholeObjList, ref targetWholeZ, ref cubeDirWholeZ, ref targetRotationWholeZ, ref isZAntiClockwise);
    }

    public void UpdateZAxisClockwise()//Z轴正向旋转
    {
        UpdateRotateZEvent(Vector3.forward, cubeDirWholeZ, targetRotationWholeZ, RotateModel.Forward, ref WholeZContent, ref isZClockwise, ref isZAntiClockwise);
    }

    public void UpdateZAxisAntiClockwise()//Z轴反向旋转
    {
        UpdateRotateZEvent(Vector3.back, cubeDirWholeZ, targetRotationWholeZ, RotateModel.Back, ref WholeZContent, ref isZAntiClockwise, ref isZClockwise);
    }

    //封装点击事件
    public void ClickBtnEvent(RotateModel model, Axis axis, ref GameObject nowContent,ref List<GameObject> objList,ref int targetDegree,ref Direction cubeDir,ref Quaternion targetRotation,ref bool isRotateStart)
    {
        if (isPlaying)
            return;
        foreach (GameObject cubeObj in objList)
        {
            cubeObj.transform.SetParent(nowContent.transform);
        }
        
        targetDegree += (int)model;
        int index = targetDegree % 360;
        if (index == -270)
            index = 90;
        if (index == 270)
            index = -90;
        if (index == 180)
            index = -180;
        //Debug.Log("Dir:" + cubeDir);
        cubeDir = (Direction)index;
        switch (axis)
        {
            case Axis.X:
                targetRotation = Quaternion.Euler((float)cubeDir, 0, 0);
                break;
            case Axis.Y:
                targetRotation = Quaternion.Euler(0, (float)cubeDir, 0);
                break;
            case Axis.Z:
                targetRotation = Quaternion.Euler(0, 0, (float)cubeDir);
                break;
            default:
                break;
        }
        if (nowContent.GetComponent<Rigidbody>() != null)
            Destroy(nowContent.GetComponent<Rigidbody>());
        isRotateStart = true;
        prevContent = nowContent;
    }

    //魔方旋转(x轴)
    public void UpdateRotateXEvent(Vector3 rotateDir, Direction cubeDir, Quaternion targetRotation, RotateModel model, ref GameObject nowContent, ref bool isRotateStart, ref bool isRotateAntiStart)
    {
        if (isRotateStart)
        {
            isPlaying = isRotateStart;
            nowContent.transform.Rotate(rotateDir * Time.deltaTime * rotateSpeed);
            //Debug.Log("X:" + nowContent.transform.localRotation.x);
            //Debug.Log("X2:" + targetRotation.x);
            bool isBreak = false;

            switch (model)
            {
                case RotateModel.Forward:
                    {
                        switch (cubeDir)
                        {
                            case Direction.First:
                                {
                                    if (nowContent.transform.localRotation.x >= targetRotation.x)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Second:
                                {
                                    if (nowContent.transform.localRotation.x >= -targetRotation.x - 0.0001f)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Third:
                                {
                                    if (nowContent.transform.localRotation.x >= targetRotation.x)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Fourth:
                                {
                                    if (nowContent.transform.localRotation.x >= targetRotation.x)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                        }
                    }
                    break;
                case RotateModel.Back:
                    {
                        switch (cubeDir)
                        {
                            case Direction.First:
                                {
                                    if (nowContent.transform.localRotation.x <= targetRotation.x)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Second:
                                {
                                    if (nowContent.transform.localRotation.x <= targetRotation.x + 0.0001f)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Third:
                                {
                                    if (nowContent.transform.localRotation.x >= -targetRotation.x)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Fourth:
                                {
                                    if (nowContent.transform.localRotation.x <= targetRotation.x)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                        }
                    }
                    break;
            }

            if (isBreak)
            {
                nowContent.transform.localRotation = targetRotation;
                isRotateStart = false;
                isRotateAntiStart = false;
                Rigidbody rigidbody = nowContent.AddComponent<Rigidbody>();
                rigidbody.useGravity = false;
                isPlaying = isRotateStart;
            }

        }
    }

    //魔方旋转(y轴)
    public void UpdateRotateYEvent(Vector3 rotateDir, Direction cubeDir, Quaternion targetRotation, RotateModel model, ref GameObject nowContent, ref bool isRotateStart, ref bool isRotateAntiStart)
    {
        if (isRotateStart)
        {
            isPlaying = isRotateStart;
            nowContent.transform.Rotate(rotateDir * Time.deltaTime * rotateSpeed);
            bool isBreak = false;

            switch (model)
            {
                case RotateModel.Forward:
                    {
                        switch (cubeDir)
                        {
                            case Direction.First:
                                {
                                    if (nowContent.transform.localRotation.y >= targetRotation.y)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Second:
                                {
                                    if (nowContent.transform.localRotation.y >= -targetRotation.y - 0.0001f)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Third:
                                {
                                    if (nowContent.transform.localRotation.y >= targetRotation.y)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Fourth:
                                {
                                    if (nowContent.transform.localRotation.y >= targetRotation.y)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                        }
                    }
                    break;
                case RotateModel.Back:
                    {
                        switch (cubeDir)
                        {
                            case Direction.First:
                                {
                                    if (nowContent.transform.localRotation.y <= targetRotation.y)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Second:
                                {
                                    if (nowContent.transform.localRotation.y <= targetRotation.y + 0.0001f)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Third:
                                {
                                    if (nowContent.transform.localRotation.y >= -targetRotation.y)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Fourth:
                                {
                                    if (nowContent.transform.localRotation.y <= targetRotation.y)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                        }
                    }
                    break;
            }

            if (isBreak)
            {
                nowContent.transform.localRotation = targetRotation;
                isRotateStart = false;
                isRotateAntiStart = false;
                Rigidbody rigidbody = nowContent.AddComponent<Rigidbody>();
                rigidbody.useGravity = false;
                isPlaying = isRotateStart;
            }

        }
    }

    //魔方旋转(z轴)
    public void UpdateRotateZEvent(Vector3 rotateDir, Direction cubeDir, Quaternion targetRotation,RotateModel model, ref GameObject nowContent, ref bool isRotateStart, ref bool isRotateAntiStart)
    {
        if (isRotateStart)
        {
            isPlaying = isRotateStart;
            nowContent.transform.Rotate(rotateDir * Time.deltaTime * rotateSpeed);
            bool isBreak = false;

            switch (model) {
                case RotateModel.Forward:
                    {
                        switch (cubeDir)
                        {
                            case Direction.First:
                                {
                                    if (nowContent.transform.localRotation.z >= targetRotation.z)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Second:
                                {
                                    if (nowContent.transform.localRotation.z >= -targetRotation.z - 0.0001f)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Third:
                                {
                                    if (nowContent.transform.localRotation.z >= targetRotation.z)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Fourth:
                                {
                                    if (nowContent.transform.localRotation.z >= targetRotation.z)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                        }
                    }
                    break;
                case RotateModel.Back:
                    {
                        switch (cubeDir)
                        {
                            case Direction.First:
                                {
                                    if (nowContent.transform.localRotation.z <= targetRotation.z)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Second:
                                {
                                    if (nowContent.transform.localRotation.z <= targetRotation.z + 0.0001f)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Third:
                                {
                                    if (nowContent.transform.localRotation.z >= -targetRotation.z)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                            case Direction.Fourth:
                                {
                                    if (nowContent.transform.localRotation.z <= targetRotation.z)
                                    {
                                        isBreak = true;
                                    }
                                }
                                break;
                        }
                    }
                    break;
            }
            
            if (isBreak)
            {
                nowContent.transform.localRotation = targetRotation;
                isRotateStart = false;
                isRotateAntiStart = false;
                Rigidbody rigidbody = nowContent.AddComponent<Rigidbody>();
                rigidbody.useGravity = false;
                isPlaying = isRotateStart;
            }

        }
    }
}

FrontContentTrigger.cs

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

public class FrontContentTrigger : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if(!GameController.Instance.FrontObjList.Contains(other.gameObject))
                GameController.Instance.FrontObjList.Add(other.gameObject);
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if(GameController.Instance.FrontObjList.Contains(other.gameObject))
                GameController.Instance.FrontObjList.Remove(other.gameObject);
    }

}

BackContentTrigger.cs

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

public class BackContentTrigger : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (!GameController.Instance.BackObjList.Contains(other.gameObject))
                GameController.Instance.BackObjList.Add(other.gameObject);   
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (GameController.Instance.BackObjList.Contains(other.gameObject))
                GameController.Instance.BackObjList.Remove(other.gameObject);
    }

}

UpContentTrigger.cs

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

public class UpContentTrigger : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (!GameController.Instance.UpObjList.Contains(other.gameObject))
                GameController.Instance.UpObjList.Add(other.gameObject);
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (GameController.Instance.UpObjList.Contains(other.gameObject))
                GameController.Instance.UpObjList.Remove(other.gameObject);
    }

}

DownContentTrigger.cs

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

public class DownContentTrigger : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (!GameController.Instance.DownObjList.Contains(other.gameObject))
                GameController.Instance.DownObjList.Add(other.gameObject);
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (GameController.Instance.DownObjList.Contains(other.gameObject))
                GameController.Instance.DownObjList.Remove(other.gameObject);
    }

}

LeftContentTrigger.cs

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

public class LeftContentTrigger : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (!GameController.Instance.LeftObjList.Contains(other.gameObject))
                GameController.Instance.LeftObjList.Add(other.gameObject);
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (GameController.Instance.LeftObjList.Contains(other.gameObject))
                GameController.Instance.LeftObjList.Remove(other.gameObject);
    }

}

RightContentTrigger.cs

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

public class RightContentTrigger : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (!GameController.Instance.RightObjList.Contains(other.gameObject))
                GameController.Instance.RightObjList.Add(other.gameObject);
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Cube")
            if (GameController.Instance.RightObjList.Contains(other.gameObject))
                GameController.Instance.RightObjList.Remove(other.gameObject);
    }

}

写完脚本以后,我们把GameController脚本挂到MainCamera上,然后吧场景中对应的游戏物体拖上去,如下图:

接着,把FrontContent到RightContent对应的触发器脚本挂上,同时,需要注意的是,要把27个Cube游戏物体的tag值,改成“Cube”。整个魔方的控制,就算完成了。

按上面的按钮可以整体转动魔方,按下面的按钮可以转动单独的某个魔方面。

以上。

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 30
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值