如何克隆GameObject(立方体)并向前或向后移动?

时间:2017-03-28 01:14:27

标签: c# unity3d unity5

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

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? 克隆人想要关闭墙壁。

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

Walls

现在我想添加两个墙来像建筑一样关闭它。 我喜欢让它移动(缩放比例),就像我现在使用立方体一样。

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

更新

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

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(gameObjectToRaise, new Vector3(i * 1.0f, lp.y, lp.z), Quaternion.identity);

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

1 个答案:

答案 0 :(得分:0)

尝试添加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);

注意&#34; +运算符&#34;我在代码示例中使用过。