+1 vote

In 2D, can the child of a node be resized to fit it's parent? In my case, the sprite child to fit the area2d parent

in Engine by (42 points)

1 Answer

+1 vote

It depends on what the child and the parent are, not all nodes have a size of their own.

For example, you can change the size of a sprite by modifying its scale property.
Then, you need to know the size of your Area2D: this depends on the collision shape you used. If it's a rectangle, you would need to adjust the scale of your sprite in such a way the resulting size is the same as the rectangle. Finally, your sprite needs to be centered on the Area2D.

Probably something like this:

# Assuming the area has a child CollisionShape2D with a RectangleShape resource
var area_size = area.get_node("CollisionShape2D").shape.extents * 2.0

# The size of a sprite is determined from its texture
var texture_size = sprite.texture.get_size()

# Calculate which scale the sprite should have to match the size of the area
var sx = area_size.x / texture_size.x
var sy = area_size.y / texture_size.y

sprite.scale = Vector2(sx, sy)

But this may be wildly different depending on which nodes you have and how they are structured.

by (28,966 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.