Building/Construction within unity

Hi there,
i’m currently trying to implement a construction/building system into my unity game as this will be one of the fundamental features that the game will entail.

I’m a fairly seasoned C# programmer and studied software programming at college but i have only been programming in a game context for the last 8 months.

I’ve created a script that does the following:

  • (on left mouse button down )Sends a raycast in order to find the position the player is looking towards
  • Instantiating an object at that position (thus building)

THE PROBLEMS!

  1. I currently cannot figure out how to build objects adjacent to each other, after numerous attempts i’ve decided to come to the forums and yes! i have already searched for answers, nothing quite fits the bill :slight_smile:

  2. if i’m looking at the top of an object the next object i build should be placed on top of it inline with the bottom object.

I do hate to use the Unity forums because of the amount of duplicates i seem to find when browsing, i don’t wish to add to that however i’m feeling rather desperate after a number of attempts.

If anyone can either write up a simplistic snapping system/ building script i would be highly grateful although i would be more than happy with an explanation as to how to do this.

the script may clean up a few things, i know it’s been bugging quite a few people. :slight_smile:

Thats a pretty generic question at the moment so I can only provide generic answers…

Snapping

  • Maybe start with snapping to a grid, that is very simple. Just round your positions using int casting to use the Mathf functions.
  • For snapping next to another object you need to do a few things:
    a) Find if an object is within range for snapping (you could cast rays or just cycle with a list of all created objects).
    b) Snapping may be required in one or two axis’ so store both.
    c) Find the boundaries of the object(s) to be snapped to (either with raycasts or by checking mesh/collider bounds). Place your new object in a position which is offset from the objects boundaries by the new objects size in the relevant axis (for example if you are snapping on X your X position should be snapTarget.XBoundary + (newObject.xSize /2))

Place On Top

Not quite sure what your issue is here…

If your objects are cuboids you can use the mesh bounds or collider bounds to get the height of the object. The centre of the new object should be the old objects centre with the height axis (probably y) replaced with the height of the previous object + 1/2 the height of the new object (assuming objects have their pivot at centre).

If you want to snap to any surface on any angle well this is a harder problem. You would need to get the closest surface and then rotate the new object to align with the surface normal. Height would be based on the position of the normal with the object offset 1/2 its height along the surface normal.


There’s not much that should be a suprise in there but maybe writing out steps will help you break it down. If you have a more specific question fire away.

Thanks dude, I’ve been awake for about 26 hours so far pretty much solid programming and i’m experiencing some writers block! hehe, Sorry if i came across as vague, that was far from my intention.

So far your answers have been more than adequate however not all of my objects are cuboid, they vary in shape and size, this is the problem with the snapping/placement as i can’t use a standardized script to place various items. I know there’s a way around it by storing each objects bounds/sizes to a variable and then adjusting the placement based on size etc i just can’t figure it out currently.

If push comes to shove i’ll probably end up paying someone to sort it out for me as i’ve got so much to do in so little time!

Thanks for your help anyways Johnny! :slight_smile: