- Home /
how to scale object to fit inside a room
Problem: I have a room with 4 walls and 1 Ceiling and a floor. Now, there is a Cube to calculate the carpet area.
The cube should hit the wall colliders
so that I can always display dimensions of each wall always in the center by calculating the centre of the carpetArea size calculator
Can someone please help me write this code?
Answer by Harinezumi · Apr 16, 2018 at 08:16 AM
Assuming that the images show a view from above, so that x axis points to the right, and z axis points up (also assuming that the walls are not rotated, but scaled), you calculate the center coordinates of the room using the coordinates and thickness of the wall objects, you average the coordinates to get the center of the carpet cube, and scale it by the difference of the positions:
float roomMinX = wallD.transform.position.x + wallD.transform.localScale.x * 0.5f;
float roomMaxX = wallB.transform.position.x - wallB.transform.localScale.x * 0.5f;
float roomMinZ = wallC.transform.position.z + wallC.transform.localScale.z * 0.5f;
float roomMaxZ = wallA.transform.position.z - wallA.transform.localScale.z * 0.5f;
GameObject carpetCube = ...; // get the cube from somewhere
carpetCube.transform.position = new Vector3 ((roomMinX + roomMaxX) * 0.5f, 0 /* or floor height of the room */, (roomMinZ + roomMaxZ) * 0.5f);
carpetCube.transform.localScale = new Vector3 (roomMaxX - roomMinX, carpetThickness, roomMaxZ - roomMinZ);
This should get you started along the right path, figuring out the rest of the details along the path.
Your answer
Follow this Question
Related Questions
Ignore Collision Temporarily 1 Answer
Simple Maths Problem - Help! 1 Answer
scalefactor issue 0 Answers
Collider bounds size is 0 0 Answers