- Home /
Found the solution
Scaling a cube side by a plane length
So I'm in school learning how to code and make games etc, and I'm trying to work on a small personal project via Unity. So basically I started setting up some basics of a game with some 3D rooms, nothing fancy, going to be top down 2.5D.
So I decided if I could make a room prefab that would make my life a lot easier. So I put down a plane, and made several cubes a child of the plane, then manually scale the to the correct positions.
So then I wanted the walls to move and scale with the plane to be at the correct locations and be the correct length and width. So I wrote this script, which right now mainly scales the cube's width to always be the same no matter what the scale of the plane is.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class Resizing : MonoBehaviour {
[SerializeField]
Transform Floor;
[SerializeField]
int X1Y2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float X1Scaling = 10.3f - ((Floor.localScale.z - 1) * 0.1f); //z
float Y2Scaling = 10.3f - ((Floor.localScale.x - 1) * 0.1f); //x
if (X1Y2==1)
transform.localScale = new Vector3(0.3f/Floor.localScale.x,transform.localScale.y, X1Scaling); //z
if (X1Y2 == 2)
transform.localScale = new Vector3(0.3f / Floor.localScale.z, transform.localScale.y, Y2Scaling); //x
}
}
So now I want to make it so the length of the cubes are always meeting up at the ends, and when the scale of a side of the plane is at 1, the correct scale for a cube is 10.3. So I tried several different things to make this work, but it is always slightly under or overscaled. (See below).
So I was wondering if anyone had a good idea on how I can scale this properly?
Floor scale of 1
Floor scale of 12