# snapping prefabs to a coordinate at runtime question

Hey all,

I’m currently making an rts type of game where you can buy buildings and place them as well as move them around. My question is if it is possible to set up specified coordinates (x,y,z) along a pre existing road so that if you move the building close to that point that it would snap to it so you can place it there. Any help would be appreciated. Thanks.

Hello, sure, it is definitely possible. What you have to do is cache those positions in a Vector3 array and then run a Distance Check from your building to those positions (use a “for” loop to iterate). You’ll need a Proximity variable which will be used to say “if distance < Proximity, building position = cached position!”.

If you have a lot of positions to cache, this might run pretty slow. If you need further optimizations, i can suggest you a few

hi thanks for the reply that is definitely gonna be helpful i will surely try it and see how it goes. But yea i most likely will quite a few positions to cache since there is lots of building placement involved. I would certainly be interested in those optimizations as well :). Thanks for your help though.

I would just put a primitive collider at each point that can be snapped to and when in “building placement mode” do a sphere cast and snap to the position of the closest collider.

Thanks for your reply Kelso. ok so bascially when the building with the spherecast detects two other colliders it would know which one is closer through that spherecast and then i would just have to change the position of the building from the current to one of the colliders. Is that correct?

Sphere casts can return you all the colliders it detected and from that you can get the position of each collider. Once you have the positions, determining the distance to each (and thus the closest one) is easy enough.

sounds great I will try it out for sure. Thanks for your help!

I tried Ted’s method and that works great for me. I’m wondering now how to remove a point from the vector3 array after a building has been placed on it so that i dont end up placing another building on the same spot? Here is the code:

``````			float proximity = 50;
Vector3[] snappoints = new[] {new Vector3(1000,0,900),new Vector3(1100,0,900)};
foreach(Vector3 snappoint in snappoints)
{
float dist = Vector3.Distance(currentBuilding.position,snappoint);
if(dist < proximity)
{
currentBuilding.transform.position = snappoint;
}
}
``````

Simplest way is to add an ‘occupied’ flag, or the currentbuilding to the snappoints array.

If you want to remove it, i’d use a List.

Take a look here :

``````List<Vector3> myList = new List<Vector3>(); //Declare it like so. If all of your positions are precalculated, you can add them directly here in the paranthesis. For example : List<Vector3> myList = new List<Vector3>(Vector3(1,1,1), Vector3(2,2,2));

myList.RemoveAt(index); //Use this to remove an element. You'd need to use a normal "for" loop with a counter, tho... and the counter will be your input :)
``````

But if you destroy the building, you will have to re-add the position to the list, so you can place another one in the same spot, i suppose. Or, as RalphTrickey said, you could have a pre-cached Vector3 built-in array with all the positions, and a List containing the available indices. Experiment around and see which fits you best.

Best of luck!

Oh, and about the optimizations… I don’t think you have to worry about it unless you’re gonna have tens of thousands of points being checked constantly.

First of all, the easiest and the most important one is not to run the check every frame. I don’t think it is needed. Every 0.1 seconds or 0.05 seconds should be enough. Or you could break down the checking process into waves of 256 checks, let’s say, then yield for a frame and continue.

The second thing would be what you have just asked. Do not check the points where there are buildings placed.

You can also simplify the distance checking formula, i believe… dist = (V1 - V2).magnitude where v1 and v2 are two different vector3s. Let’s note V1-V2 as R. So dist = Mathf.Sqrt(R.xR.x + R.yR.y + R.z*R.z).
And Mathf.Sqrt(R.xR.x + R.yR.y + R.z*R.z) has to be < than Proximity. So
R.xR.x + R.yR.y + R.zR.z < ProximityProximity. So there you go, you just got rid of the Mathf.Sqrt which would be the most expensive operation you have to do here. Now you have 3 multiplications (since Proximity squared can be pre-calculated) and 3 additions to do every check. Don’t worry, the computer can calculate quite a fucking lot of these every nanosecond.

Lastly, if performance is your main concern, you could make a binary-tree/ quad-tree -like distance checking. But i won’t go into detail on this since you would only need this kind of optimization if you literally have hundreds of thousands of checks to perform.

thanks alot Ted thats some very helpful stuff ill be sure to try it out when i get some free time thanks again