我怎样才能将立方体缩放到只剩下一个方向?

How can i scale a cube to only one direction left?

然后只能向右,只能向上,只能向下。 主要思想是向一个方向扩展。 问题是我没有使用编辑器来添加新的空游戏对象或立方体。 我正在使用脚本创建一个立方体和一个新的空游戏对象。

在场景的编辑器中window我已经有了一个立方体。在脚本中,我将这个立方体用于 create/duplicate 另一个立方体并创建新的空游戏对象。

我试着像这里的答案一样工作:解决方法2。

但是这个解决方案使用的是编辑器,而我使用的是脚本。 到目前为止我尝试了什么:

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;
        go = new GameObject();
        CreateCubes();
    }

    void Update()
    {
        if (DetectPlayer.touched == true)
        {
            if (raiseAmount < raiseTotal)
            {
                float raiseThisFrame = speed * Time.deltaTime; 
                if (raiseAmount + raiseThisFrame > raiseTotal)
                {
                    raiseThisFrame = raiseTotal - raiseAmount;
                }
                raiseAmount += raiseThisFrame;

                gameObjectToRaise.transform.localScale += new Vector3(raiseThisFrame, raiseThisFrame, 0);
                go.transform.localScale += new Vector3(raiseThisFrame, raiseThisFrame, 0);
                go.transform.position += Vector3.left * (speed / 2) * Time.deltaTime;
            }
            else
            {
                raised = true;
            }
        }
    }

    private List<GameObject> CreateCubes()
    {
        cubes = new List<GameObject>();
        for (int a = 0; a < x; a++)
        {
            for (int b = 0; b < z; b++)
            {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                if (randomColor)
                {
                    cube.GetComponent<Renderer>().material.color = colorChoices[Random.Range(0, (colorChoices.Length))];
                }
                cube.transform.position = new Vector3(lp.x - 1, lp.y, lp.z);
                cube.transform.Rotate(0, 90, 0);

                cubes.Add(cube);
            }
        }

        go.transform.position = new Vector3(lp.x - 2,lp.y,lp.z);
        go.transform.Rotate(0, 90, 0);
        cubes[0].transform.parent = go.transform;

        return cubes;
    }
}

但它再次在左右两侧缩放 go var(新的空游戏对象),我只想要左侧。

在我的第一个问题中,它被标记为已回答,但 link 中的解决方案是在使用编辑器时,我想通过脚本来完成。

In my first question it was marked as already answered but the in the link is when using the editor and i want to do it by script.

仍然是the-same解决方案。创建一个立方体,然后创建一个空的 GameObject。将空 GameObject 放置在立方体的左侧。现在用 childCube.transform.SetParent(thatObject.transform); 使那个立方体成为 GameObject 的 child 一个。就是这样。

下面是执行此操作的助手 class。它可以将枢轴点定位到任何位置。我提供了 CubePivotPoint 枚举以使其更容易。您也可以传入 Vector3 位置作为轴心点。

public class CUBE
{
    public enum CubePivotPoint
    {
        MIDDLE, LEFT, RIGHT, UP, DOWN, FORWARD, BACK
    }

    //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);
            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;
    }
}

用法:

void Start()
{
    GameObject cube = CUBE.CreatePrimitive(CUBE.CubePivotPoint.RIGHT);

    StartCoroutine(scaleCube(cube.transform));
}

IEnumerator scaleCube(Transform trans)
{
    while (true)
    {
        trans.localScale += new Vector3(0.1f, 0, 0);
        yield return null;
    }
}

至于将其集成到您当前的代码中,更改

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

GameObject cube = CUBE.CreatePrimitive(CUBE.CubePivotPoint.RIGHT);.

然后将 cube.GetComponent<Renderer>() 更改为 cube.GetComponentInChildren<Renderer>(),因为立方体现在是另一个游戏 Object.

的 child Object