如何克隆游戏对象(立方体)并将其向前或向后移动?

How can I clone a GameObject (cube) and move it forward or backward?

克隆我的意思是还包括缩放立方体的比例。

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

public class Test : MonoBehaviour
{
    public GameObject gameObjectToRaise;
    public float raiseAmount;
    public float raiseTotal = 50;
    public float speed = 2;
    public static bool raised = false;

    public int x, z;
    private List<GameObject> cubes;
    private GameObject go;

    public bool randomColor;
    public Color[] colorChoices;

    Vector3 lp;
    Vector3 ls;

    // Use this for initialization

    void Start()
    {
        lp = gameObjectToRaise.transform.localPosition;
        ls = gameObjectToRaise.transform.localScale;

        GameObject cube = CUBE.CreatePrimitive(CUBE.CubePivotPoint.UPLEFT);
        GameObject cube1 = CUBE.CreatePrimitive(CUBE.CubePivotPoint.UPRIGHT);

        cube.transform.position = new Vector3(lp.x, lp.y, lp.z);
        cube1.transform.position = new Vector3(lp.x, lp.y, lp.z);
        cube1.transform.Rotate(0, 90, 0);

        StartCoroutine(scaleCube(cube.transform));
        StartCoroutine(scaleCube(cube1.transform));

        GameObject clone1 = Instantiate(cube);
        GameObject clone2 = Instantiate(cube1);

        clone1.transform.localScale = cube.transform.localScale;
        clone2.transform.localScale = cube1.transform.localScale;
        clone1.transform.position = new Vector3(lp.x, lp.y, lp.z - 15);
        clone2.transform.position = new Vector3(lp.x - 15, lp.y, lp.z);
    }

    IEnumerator scaleCube(Transform trans)
    {
        while (raiseAmount < raiseTotal)
        {
            raiseAmount += 1;
            trans.localScale += new Vector3(speed * Time.deltaTime, speed * Time.deltaTime, 0);
            yield return null;
        }
    }

    public class CUBE
    {
        public enum CubePivotPoint
        {
            MIDDLE, RIGHT, LEFT, UP, DOWN, FORWARD, BACK, UPLEFT,
            UPRIGHT, FORWARDUP, BACKUP
        }

        //Takes CubePivotPoint Enum as pivot point
        public static GameObject CreatePrimitive(CubePivotPoint pivot)
        {
            //Calculate pivot point
            Vector3 cubePivot = createPivotPos(pivot);

            //Create cube with the calculated pivot point
            return createCubeWithPivotPoint(cubePivot);
        }

        //Takes Vector3 as pivot point
        public static GameObject CreatePrimitive(Vector3 pivot)
        {
            //Create cube with the calculated pivot point
            return createCubeWithPivotPoint(pivot);
        }

        private static Vector3 createPivotPos(CubePivotPoint pivot)
        {
            switch (pivot)
            {
                case CubePivotPoint.MIDDLE:
                    return new Vector3(0f, 0f, 0f);
                case CubePivotPoint.LEFT:
                    return new Vector3(0.5f, 0f, 0f);
                case CubePivotPoint.RIGHT:
                    return new Vector3(-0.5f, 0f, 0f);
                case CubePivotPoint.UP:
                    return new Vector3(0f, 0.5f, 0f);
                case CubePivotPoint.DOWN:
                    return new Vector3(0f, -0.5f, 0f);
                case CubePivotPoint.FORWARD:
                    return new Vector3(0f, 0f, 0.5f);
                case CubePivotPoint.BACK:
                    return new Vector3(0f, 0f, -0.5f);
                case CubePivotPoint.UPLEFT:
                    return new Vector3(0.5f, -0.5f, 0);
                case CubePivotPoint.UPRIGHT:
                    return new Vector3(-0.5f, -0.5f, 0);
                case CubePivotPoint.FORWARDUP:
                    return new Vector3(0.5f, -0.5f, -0.5f);
                case CubePivotPoint.BACKUP:
                    return new Vector3(0f, -0.5f, -0.5f);
                default:
                    return default(Vector3);
            }
        }

        private static GameObject createCubeWithPivotPoint(Vector3 pivot)
        {
            //Create a cube postioned at 0,0,0
            GameObject childCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            //Create an empty parent object
            GameObject parentObject = new GameObject("CubeHolder");
            //Move the parent object to the provided pivot postion 
            parentObject.transform.position = pivot;
            //Make the childcube to be child child of the empty object (CubeHolder)
            childCube.transform.SetParent(parentObject.transform);
            return parentObject;
        }
    }
}

在枚举中我添加了 UPLEFT 和 UPRIGHT。

问题是支点。现在我想克隆这两堵墙或立方体,并将它们分别移动到另一个方向,这样就有 4 堵墙了。

或者也许我根本不需要克隆而不是 UPLEFT 和 UPRIGHT? 我想用克隆人来关闭墙壁。

没有克隆部分我得到的是:

现在我想再添加两堵墙来像建筑物一样关闭它。 我更喜欢让它移动(比例变化),就像我现在对立方体所做的那样。

问题是如何添加下两堵墙?另一个支点?使用相同的枢轴?制作一个克隆体并放置两堵墙?

更新

到目前为止我尝试的是在我做的开始功能中:

void Start()
    {
        for (int i = 0; i < 10; i++)
        {
            Instantiate(gameObjectToRaise, new Vector3(i * 1.0f, lp.y, lp.z), Quaternion.identity);
            Instantiate(gameObjectToRaise, new Vector3(lp.x, i * 1.0f, lp.z), Quaternion.identity);
        }
    }

第二个 Instantiate 工作正常,新游戏对象来自 gameObjectToRaise。但是Instantiate的第一行:

Instantiate(gameObjectToRaise, new Vector3(i * 1.0f, lp.y, lp.z), Quaternion.identity);

将新的 gameonects 放在地形上不靠近 gameObjectToRaise 的另一个位置。这是为什么?

尝试添加gameObjectToRaise的位置如下:

Instantiate(gameObjectToRaise, new Vector3(lp.x + i * 1.0f, lp.y, lp.z), Quaternion.identity);
Instantiate(gameObjectToRaise, new Vector3(lp.x, lp.y + i * 1.0f, lp.z), Quaternion.identity);

注意我在代码示例中使用的“+ 运算符”。