How can calculate and change the parent gameobject Y position to be exactly height on terrain ?

I have in the Hierarchy a empty GameObject called it Walls
Then as childs of Walls i added 4 empty GameObjects
Then for each empty GameObject i added a child cube

For example if i give the size of the walls on the variable wallsSize to be 500 then it will build a 500x500 square.

The problem is when it’s doing the square some of the walls in another Y position.
For example the empty gameobject Walls is at position 33,0,0

But then if i take for example GameObject (1) or GameObject i see it’s Y position is -6
But if i take for example GameObject (2) or GameObject (3) i see they both Y position at 0

That make two walls smaller then the two others. Two walls at position 0 height and two at -6.
Not sure why and how to fix it.
I want all 4 walls GameObject , GameObject (1) , GameObject (2) , GameObject (3) to be at height 0 Y = 0

The script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walls : MonoBehaviour
{
    public Vector3 scaleFactor;
    public float wallsSize;
    public bool scaleDown = false;

    private bool changepos = false;

    private void Update()
    {
        if (scaleDown == false)
        {
            if (transform.localScale.x != wallsSize && transform.localScale.z != wallsSize)
            {
                transform.localScale += new Vector3(scaleFactor.x, scaleFactor.y, scaleFactor.z);
            }
        }

        if (scaleDown == true)
        {
            if (changepos == false)
            {
                transform.position = new Vector3(transform.position.x + wallsSize,0, transform.position.z + wallsSize);
                changepos = true;
            }

            if (transform.localScale.x != -wallsSize && transform.localScale.z != -wallsSize)
            {
                transform.localScale -= new Vector3(scaleFactor.x, scaleFactor.y, scaleFactor.z);
            }
        }
    }
}

I find your use of transform.localScale to be confusing. Why not simply set it instead of using += or -=?

By default the scale is 1,1,1. You’re adding scaleFactor to 1,1,1 rather than simply setting it?

Not only that, I’m surprised that you got away with using != when comparing the x value to wallSize, due to floating point inaccuracies.

1 Like

Could you show me example how to code should be like ? Something to start with.
The main idea is to build walls square/rectangle on specific given area. For example if i will type in some public Vector3 variable start position 0,0,0 the walls will start from 0,0,0 on the terrain.

If i change the variable for example to 100,0,100 the walls will start at 100,100 position on the terrain.

I guess I’m confused about what you’re trying to do and why you’re using this approach to place the walls.

If you’re (for example) building a Room with walls, why not just use unit scales to create the walls (i.e. 1,0.01,1 to make a floor, etc.), then scale the Room itself to 500,500,500 or however much you want the scale factor to be?

1 Like

I wanted to build the room automatic.

I got a little lost in the extra comments, but are you looking for this? Unity - Scripting API: Terrain.SampleHeight

That’s what first came to mind when I read your thread topic. :slight_smile:

1 Like

Thanks.

No problem.