ColoRect won't line up with CollisionShape2D
Asked Answered
B

3

0

Trying to get a bunch of ColorRects to cover the Collisionshape2Ds i set up. Creating them does work, but all of them are slightly off from where they should be. I checked their sizes, positions scale, etc. in the Inspector, they're all identical and should line up. Even on Remote View i don't see what could possibly cause this. My hunch was since ColorRects are Control Nodes (and Control nodes are always weird), that there is something wrong with the anchors or the pivot, but i don't see anything off there either... here's the code in any case:

func _ready():
	for collision_shape in get_children():
		var wall = ColorRect.new()
		wall.set_size(collision_shape.get_shape().get_size())
		wall.global_position = collision_shape.position
		wall.rotation = collision_shape.rotation
		wall.scale = collision_shape.scale
		get_tree().get_first_node_in_group("WALL").add_child.call_deferred(wall)
		print(wall.get_begin())


The ColorRects instantiate on the Control node as they should. I tried instantiating them on the StaticBody (called "Walls" in the Scene Tree) as well, that gives me the same result, every ColorRect is slightly off.


Burg answered 22/2, 2024 at 18:27 Comment(0)
A
1

The problem is you're using Control nodes. These are meant for UI elements and not necessarily 2d graphics so they work differently than the 2d nodes. I would just make a simple rectangle sprite, put it where needed and adjust as needed and do this with 2d nodes for each instance. I would also make the sprites children of each wall piece.

Though why are you hiding your collision shapes? The editor should do this by default. Attaching the sprites to the walls as children in the SceneTree can also help while editing.

Arlinearlington answered 22/2, 2024 at 20:10 Comment(0)
B
0

Arlinearlington That's all fair, i just wanted to see if i could do it this way. It's more for fun & practice than actual in-game application. And more importantly, i'd really like to understand what exactly is causing the ColorRects to be askew here.

I've been playing with other code to create ColorRects on ready() via loops and such, and that all works fine. This here really trips me up though...

Burg answered 23/2, 2024 at 5:58 Comment(0)
A
0

Burg FWIW I try not to use ColorRect too much mainly because of the reason you mentioned. They never seem to be where I need them on ready() unless it's perhaps a single rect I'm using for say, shaders.

Arlinearlington answered 23/2, 2024 at 19:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.