in game snap to grid

hey everyone,

how can i go about making an object have a snap to grid feature while in game?

my current solution is to space apart empty game objects evenly around the level where snap is needed, but this can come out to be A LOT of points.

say i need the object to be able to be snapped every 1 unit, is there a more code efficient way to go about this?

thanks

1 Like
var currentPos = transform.position;
transform.position = Vector3(Mathf.Round(currentPos.x),
                             Mathf.Round(currentPos.y),
                             Mathf.Round(currentPos.z));

–Eric

11 Likes

Impressive.

awesome! ill check it out thanks

Mathf… There we meet again… :stuck_out_tongue:

Thanks for the code Eric.

Note to self: Don’t over complicate simple things.

Thanks Eric.

Man… Why is everything Eric posts so simple and profound?

how can you make it snap to a bigger, wider grid?

Try this (I haven’t tested but it seemed logical to me).

    var currentPos = transform.position;
    transform.position = Vector3(Mathf.Round(currentPos.x * 10),
                                 Mathf.Round(currentPos.y * 10),
                                 Mathf.Round(currentPos.z * 10));

Or try multiply by 100 or 1000, whatever :slight_smile:

Sorry, but that’s incorrect. It’s simply the other way around.

To get an object’s position on a grid, you have to divide its position by the grid size.
So in case of a three dimensional grid, you’d calculate the position as follows:

Vector3 currentPos = transform.position;
transform.position = Vector3( Mathf.Round( currentPos.x / gridSize.x ),
                              Mathf.Round( currentPos.y / gridSize.y ),
                              Mathf.Round( currentPos.z / gridSize.z ) );
1 Like

I guess that’s how we learn thanks.

1 Like

Another option: try

Mathf.Round(currentPos.x) * grid_scaling_size;

// multiplying AFTER rounding, which will “spread out” the otherwise unit-based grid if multiplying by values greater than 1, and will “squeeze” it if multiplying by values between 0 and 1.
// grid_scaling_size could also be a random value for craziness…

Of course, that’s the great thing about coding. And I’m sorry, willemsenzo, I didn’t want to sound too harsh, arrogant or even disrespectful.

If you want objects to keep as close to their original position as possible, but with a larger grid, you could use:

var currentPos = transform.position;
transform.position = Vector3(Mathf.Round(currentPos.x / grid_scale) * grid_scale,
							 Mathf.Round(currentPos.y / grid_scale) * grid_scale,
				   			 Mathf.Round(currentPos.z / grid_scale) * grid_scale);
7 Likes

Don’t worry I don’t feel offended in any way :slight_smile:

2 Likes

Great thread, I plan to use some of the ideas on here. Could be very useful. Thanks everyone!

Just throwing in my two cents. I wrote a simple function that runs through an array of Vector2s (Or Vector3s) and returns the index of the position that is currently closest to yours.

    int CompareSet( Vector2[] myArray, Vector2 pos ){
        int closestIndex = 0;
        float smallestDistance = 0.0f;
        for( int u = 0; u < myArray.Length; u++ ){

            if( u != 0 ){

                float thisDistance = (pos - myArray[u]).sqrMagnitude;

                closestIndex =  ( thisDistance < smallestDistance ) ? u : closestIndex;
                smallestDistance = ( thisDistance < smallestDistance ) ? thisDistance : smallestDistance;

            }

            else smallestDistance = (pos - myArray[u]).sqrMagnitude;

        }

        return closestIndex;

    }
2 Likes

Still learning this. Could someone apply this in an example?

Vector3 currentPos = transform.position;
transform.position = Vector3( Mathf.Round( currentPos.x / gridSize.x ),
                              Mathf.Round( currentPos.y / gridSize.y ),
                              Mathf.Round( currentPos.z / gridSize.z ) );

Yes I’d like to second that. I’ve tried to apply this to moving a cube around the terrain according to a grid but ended up with all kinds of errors.

Well, so far all we’ve talked about here is how to round numbers (note: only Errorsatz’s post above does this correctly for a grid size other than one).

To actually apply this, we’d need to know more about what you’re trying to accomplish… I mean, sure, you can snap an object to a grid, but how does it move? Does it move at all? Do you want it to slide smoothly from one grid position to another, and then snap? Or can it just jump from one grid position to the next? Are objects falling in from outer space, and you want them to only land on even grid positions? What?

Cheers,

  • Joe