Stacking objects in runtime.

I am making a sort of simulator game. And the most important part of the game is achieving what I am failing to… stack items.

So for instance. If the user chooses a wall, when they click where they want to place it, I want to automatically place the next wall directly above it (while the mouse is hovering over, if not then it will stay at 0 on the transform position.

I have a script called ItemController and at the moment it checks to see if it is touching a trigger collider, if it is (with the tag name “item” then it moves up the height that is preset in the code)… here is an example:

void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "item")
        {
            itemPosY = other.transform.position.y + 9.5f;
        }
    }

    void OnTriggerExit(Collider other)
    {
        itemPosY = 0.05f;
    }

But it only seems to do this once. I need it to just keep moving up when I click to place the object.

Sorry if this is hard to understand. I will attach an image, if that will help…!
67904-mrt-stack-small.png

you could add a counter so that each time a wall is placed above another add 1 to that counter. Then you could go:

itemPosY = other.transform.position.y + 9.5f * counter;

You’d want the counter to initialize with a value of 1 at start.

Another way you could do it is get the bound size:

GetComponent<Collider>().bounds.size

So you don’t need to hard code the height of the object.