Trying to do a grid offset

Godot Version

4.4

Question

I am trying to implement a grid-like system where you can place stuff along the grid in its own slot.
My issue is I can’t wrap my head around how to make them offset more. I want there to be a specific offset in between them, that way if I place bigger things, there will be more room.

My issue is if I make the X/Y bigger then i’m no longer in the center of where I’m placing them, which is confusing me. Is there a better way for me to detect where in the grid a global position should be?
I really hope this is making sense, sorry if it doesn’t.

This script is attached to that white ball(which is attached to front of player) you see in the screen, it’s basically the marker for “place it here” based off the ball’s global position.

extends CSGSphere3D


func _process(delta: float) -> void:
	if Input.is_key_pressed(KEY_Q):
		var location: Vector2 = _get_grid_location(global_position)
		if owner.grid.has(location):
			return
		
		var sphere := CSGSphere3D.new()
		
		owner.grid[location] = sphere
		%GridChildren.add_child(sphere)
		
		sphere.global_position = owner.global_position + Vector3(
			location.x,
			1,
			location.y
		)
		print(sphere.global_position)
		
func _get_grid_location(location:Vector3) -> Vector2:
	return Vector2(roundf(location.x), roundf(location.z))

func _global_pos_from_grid(location:Vector2) -> Vector3:
	return Vector3(roundf(location.x) , 1, roundf(location.y))

I’m not really sure what your are trying to do but you may be able to use Vector3.snapped() to snap the global_position to a fixed step.

1 Like

Thank you! This actually helps a ton. Sorry I couldn’t word my issue better, but being able to snap it to a set step is making this work better!