I want to scale the gameobject only to the left. The way i’m doing it now it’s changing the scale to both left and right.
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;
public bool randomColor;
public Color[] colorChoices;
Vector3 lp;
Vector3 ls;
void Start()
{
lp = gameObjectToRaise.transform.localPosition;
ls = gameObjectToRaise.transform.localScale;
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);
cubes[0].transform.localScale += new Vector3(raiseThisFrame, raiseThisFrame, 0);
cubes[0].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);
}
}
return cubes;
}
}
This is the line where i scale the cube[0]
cubes[0].transform.localScale += new Vector3(raiseThisFrame, raiseThisFrame, 0);
I read that i should create a new empty gameobject and make the cube[0] child of it then to scale the empty gameobject. So i tried but it didn’t work fine.
I added a new global GameObject var called it go
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 = new GameObject();
go.transform.position = new Vector3(lp.x - 1, lp.y, lp.z);
cubes[0].transform.parent = go.transform;
}
return cubes;
}
Then i tried to scale this empty gameobject
Instead
cubes[0].transform.localScale
I tried
go.transform.localScale
But it’s not working. I tried to do it from this answer:
https://stackoverflow.com/questions/39216454/scale-gameobject-on-just-one-side
But not sure how to make it by script with my code.