Hi,

I'd like to try and convert a step-by-step 2D platformer tutorial from Unity C# to GDScript (link at bottom). Basically trying to brute-force my way through it, which may not be the best way to go for a beginner :open_mouth: But hopefully I can learn something from it at least.

I'm hitting a snag with the current piece of code, namely in the UpdateRaycastOrigins() function. I'd like to get the outer left/right, top/down coordinates of this node but apparently I can't just use self.min.x etc.

extends CollisionShape2D

const skinWidth = 0.15

var RaycastOrigins = {
	topLeft = Vector2(0,0),
	bottomLeft = Vector2(0,0),
	topRight = Vector2(0,0),
	bottomRight = Vector2(0,0)
}

func _ready():
	# Called every time the node is added to the scene.
	# Initialization here
	set_process(true)
	
func _process(delta):
	UpdateRaycastOrigins()
	

func UpdateRaycastOrigins():
	RaycastOrigins.bottomLeft = Vector2(self.min.x, self.max.y)
	RaycastOrigins.bottomRight = Vector2(self.max.x, self.max.y)
	RaycastOrigins.topLeft = Vector2(self.min.x, self.min.y)
	RaycastOrigins.topRight = Vector2(self.max.x, self.min.y)

Original Unity C# script version:

There are other problems too. There doesn't seem to be a Bounds equivalent in Godot, or maybe I'm just missing something. Any help much appreciated :smile:

Here's the tutorial link for those interested:

  1. You don't need to use self with GDScript, it's assumed.

  2. Godot has a Rect2 class if you just need an axis-aligned bounding box. It's got properties for the top left and bottom right corners, size, some convenient functions, etc. You may want to extend that directly instead of CollisionShape2D. Or you can get the rect of any CanvasItem with get_item_rect().

6 days later

get_item_rect() will get only a rect2 of (64.0, 64.0). What you need is get_item_and_children_rect() which will get you the real box surrounding your node and children if available. See issue #4738

6 years later