2017-03-26 4 views
-1

そして、右に、そして上へと下へのみに。 主なアイデアは一方向に拡大することです。 問題は、新しい空のgameobjectまたはキューブを追加するためにエディタを使用していないことです。 私は、キューブと新しい空のゲームオブジェクトを作成するためにスクリプトを使用しています。どのようにしてキューブを一方向にスケールすることができますか?

シーンウィンドウのエディタにはすでにキューブがあります。スクリプトでは、このキューブを使用して別のキューブを作成/複製し、新しい空のゲームオブジェクトを作成しています。

私はここで答えのように動作するようにしようとした:解決方法2

Solution

しかし、この解決策は、エディタを使用していると私は、スクリプトを使用しています。 は、私がこれまで試したどのような:

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

しかし、再び、それは両方に行くのVAR(新しい空のゲームオブジェクト)をスケーリングだが、左右両面と私は左にしたいです。

私の最初の質問では既に答えられているとマークされていましたが、リンクの解決策はエディタを使用するときです。スクリプトでやりたいのです。私の最初の質問で

答えて

0

それはすでに答えとしてマークされますが、エディタを使用している場合、リンクで solutionがあると私はスクリプトによってそれを をしたいました。

これはまだ同じ解決策です。キューブを作成し、空のGameObjectを作成します。キューブの左側に空のGameObjectを配置します。今、そのキューブをその子供のGameObjectchildCube.transform.SetParent(thatObject.transform);とする。それでおしまい。

以下はこれを行うためのヘルパークラスです。ピボットポイントを任意の位置に配置できます。私はそれをさらに簡単にするためにCubePivotPoint enumを提供しました。ピボットポイントとして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 = CUBE.CreatePrimitive(CUBE.CubePivotPoint.RIGHT);

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

を変更。

cube.GetComponent<Renderer>()からcube.GetComponentInChildren<Renderer>()に変更すると、キューブは別のGameObjectの子オブジェクトになります。

関連する問題