簡體   English   中英

如何克隆GameObject(立方體)並向前或向后移動?

[英]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? 隨着克隆我想關閉牆壁。

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

牆壁

現在我想添加兩個牆來像建築一樣關閉它。 我喜歡讓它移動(縮放比例),就像我現在正在使用立方體一樣。

問題是如何添加下兩個牆? 另一個支點? 使用相同的樞軸? 制作克隆並定位兩面牆?

更新

到目前為止我嘗試的是在我做的Start功能中:

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的地形上的另一個位置。 這是為什么 ?

嘗試添加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);

請注意我在代碼示例中使用的“+運算符”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM