Unity小案例(运行创建正方形Cube移动)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cube{
    public float speed = 0.2f;
    public GameObject[,] cube=new GameObject[6,6];
    public void Ins()
    {
        for (int i = 0; i < 6; i++)
        {
            for (int k = 0; k < 6; k++)
            {
                cube[i, k] = MonoBehaviour.Instantiate<GameObject>(Resources.Load("Cube") as GameObject);
                cube[i,k].transform.position = new Vector3(i * 2, 0, k * 2);
            }
        }
    }
    public void Move()
    {
      
       
        for (int i = 0; i < 6; i++)
        {
            for (int k = 0; k < 6; k++)
            {
                this.cube[i, k].transform.Translate(Input.GetAxis("Horizontal") * speed, 0, Input.GetAxis("Vertical") * speed);
                this.cube[i,k].transform.Rotate(Input.GetAxis("Horizontal") * 15,0,0 );
            }
        }
       
    }

}

下面这个我挂在了相机上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour {
    Cube cube;
       void Start () {
        cube = new Cube();
        cube.Ins();
    }
       
       // Update is called once per frame
       void Update () {
            cube.Move();
       }

}

拓展:行 列 宽 生成cube.

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TopicCheck_JCWJ : MonoBehaviour
{
    public Transform parent;
    public static bool hasFinish;
    public float startCount;//640  421
    public static float finishCount;

    private bool finish;
    void Start()
    {
        Ins();
        startCount = 200;

    }

    // Update is called once per frame
    void Update()
    {
        if (transform.childCount < 221)
        {
            hasFinish = true;
        }
        else
        {
            hasFinish = false;
        }
       


        // 检测是否完成挖掘(不存在Collider在土里的情况)
        if (CommonCheck.instance.isInWokeArea)
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                finishCount = (transform.childCount - 221) / startCount;

                if (transform.GetChild(i).GetComponent<FinishCheck>().isInSoil == false)
                {
                    Destroy(transform.GetChild(i).gameObject);
                }
            }
            Debug.Log(transform.childCount);

            Debug.Log(finishCount);
        }

        if (hasFinish)
        {
            if (finish)
            {
               
                // 切换任务
                View_TestCarUIForm._instance.ChangeTaskNEW(3);
                // 完成基础定点挖掘作业
                StaticCall.UpdateScore(3069);
                CommonCheck.instance.hasFinishAll = true;
                finish = false;
            }

        }

        //Debug.Log(transform.childCount);
    }

    // Update is called once per frame
    public float speed = 0.2f;
    public GameObject[,,] cube = new GameObject[35, 40, 5];
    public void Ins()
    {
        for (int i = 0; i < 16; i++)
        {
            for (int k = 0; k < 20; k++)
            {
                for (int j = 0; j < 2; j++)
                {
                    cube[i, k, j] = MonoBehaviour.Instantiate<GameObject>(Resources.Load("CubeCheck_JCWJ") as GameObject, parent.position, parent.rotation, parent);

                    if (cube[i, k, j].GetComponent<FinishCheck>() == null)
                    {
                        cube[i, k, j].AddComponent<FinishCheck>();
                    }
                    if (cube[i, k, j].GetComponent<Rigidbody>() == null)
                    {
                        cube[i, k, j].AddComponent<Rigidbody>().isKinematic = true;
                    }
                    cube[i, k, j].transform.localPosition = new Vector3(i * 1f, j * 0.5f, k * 1f);
                }

            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值