0
\$\begingroup\$

My application:

Place furnitures or construction objects (doors/windows) in scene.

Everything is fine until I touch the construction objects. Since the application is mobile, it will be hard for a user to place the door at the exact place, so I decided to help and put a little menu where they can specify where to put it:

Placement dialog

The scene (room in this case) looks like this: Top-down view of room

Some implementation details:

The walls are a prefab instantiated 4 times:

 GameObject wall_N = Instantiate(wallPrefab);
 wall_N.gameObject.name = "Wall_N";
 wall_N.GetComponent<Transform>().position = new Vector3(center_W, 50, center_H + z / 2);
 wall_N.transform.SetParent(room.transform, false);
 wall_N.GetComponent<Renderer>().material.color = myColor;
 wall_N.gameObject.transform.localScale = new Vector3(x, y, 0);

and so on for each wall. x, y, z are the values introduced by user and transformed into int

int x = Int32.Parse(xInput); // width int z = Int32.Parse(zInput); // length int y = Int32.Parse(yInput); // height

and Center_W and Center_H are screen points:

private int center_W = Screen.width / 2; // 960 private int center_H = Screen.height / 2; // 540

In the above image i've introduced x, y, z as 200, 200, 200

Question:

In what coordinates must I work in order to place the door at a dimension specified? I'm not even sure if this approach is correct, the main idea is to put the door at the correct position in the room.

enter image description here

What I've tried

The first idea that came in my mind was to find the bottom-left coordinates from Wall_N and place the door at point + A but then I realised that the bottom-left is represented in different coordination sistem then A. I've search about it and I tried to see the values from screenPoint in different coordinates:

var screenPoint = new Vector3(center_W, 50, center_H + z / 2);

 var worldPos = Camera.main.ScreenToWorldPoint(screenPoint);
 var screenToViewport = Camera.main.ScreenToViewportPoint(screenPoint);
 var viewpointToWold = Camera.main.ViewportToWorldPoint(screenPoint);
 var worldToViewport = Camera.main.WorldToViewportPoint(screenPoint);
        
 Debug.Log("wallN - screen point " + screenPoint + " to worldPos " + worldPos);
 Debug.Log("wallN - screen point " + screenPoint + " to viewPortPos " + screenToViewport);
 Debug.Log("wallN - viewPort " + screenPoint + " to worldPoint " + viewpointToWold);
 Debug.Log("wallN - world " + screenPoint + " to viewport " + worldToViewport);

Unfortunately, they didn't tell me anything, I just don't know how to solve this.

\$\endgroup\$

1 Answer 1

-1
\$\begingroup\$

After many tries I found that the dimensions introduced are calculated in pixels

int x = Int32.Parse(xInput); // width int z = Int32.Parse(zInput); // length int y = Int32.Parse(yInput); // height

wall_N.gameObject.transform.localScale = new Vector3(x, y, 0); -> if I introduce 200, x will be 200 pixels, y introduced will be in pixels etc relative to this position wall_N.GetComponent<Transform>().position = new Vector3(center_W, 50, center_H + z / 2);

So, after a bit of thinking I've come to this formula:

var xDistance = center_W - DimensionIntroduced/2 + dimDesiredFromWall_V + xDoorDimension/2; // or var xDistance = center_W + DimensionIntroduced/2 + dimDesiredFromWall_V + xDoorDimension/2; if i want to move at right

var yDistance = center_W + DimensionIntroduced.height/2

var clone = Instantiate(newGameObject, new Vector3(xDistance, 0, yDistance), Quaternion.identity);

So I guess that transform.localScale is measured in pixels, but i'm not very sure, as long as it works as expected in my app, it's ok xD I don't know how much it helps tho, but if somone has questions, feel free to ask.

\$\endgroup\$
3
  • \$\begingroup\$ Is this solution valid for multiple resolutions? \$\endgroup\$
    – Weckar E.
    Aug 15, 2020 at 2:10
  • \$\begingroup\$ It should be... \$\endgroup\$
    – Mara Black
    Aug 17, 2020 at 16:21
  • \$\begingroup\$ This information is not correct. Unity transform positions are measured in parent/world units, not pixels, and the pixel size will depend on how they are positioned/oriented relative to the camera, the camera's size/fov, and the resolution of the output display/window. localScale is a multiplier on size, so setting localScale to 2 will make the object twice as large as it was originally on that axis. \$\endgroup\$
    – DMGregory
    Jan 29 at 11:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .