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

限于喜欢 提交于 2019-12-12 06:39:48

问题


And then only to right and only to up and only down. The main idea is to scale to one direction. The problem is i'm not using the editor to add new empty gameobject or a cube. I'm using a script to create a cube and a new empty gameobject.

In the editor in the scene window i have a cube already. And in the script i'm using this cube to create/duplicate another cube and creating new empty gameobject.

I tried to work like the answer here: The solution method 2.

Solution

But this solution is using the editor and i'm using a script. What i tried so far:

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

But again it's scaling the go var(New empty gameobject) on both sided left and right and i want only to the left.

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


回答1:


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

It is still the-same solution. Create a cube then create an empty GameObject. Position that empty GameObject to the left side of the cube. Now make that cube a child a of that GameObject with childCube.transform.SetParent(thatObject.transform);. That's it.

Below is a helper class to do this. It can position the pivot point to any position. I provided CubePivotPoint enum to make it even easier. You can also pass in Vector3 position as a pivot point.

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

Usage:

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

As for integrating this into your current code, change

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

to

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

then change cube.GetComponent<Renderer>() to cube.GetComponentInChildren<Renderer>() since the cube is now a child Object of another GameObject.



来源:https://stackoverflow.com/questions/43035602/how-can-i-scale-a-cube-to-only-one-direction-left

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!