0 votes

When setting a texture to a sprite the size of the sprite changes according to the texture, why is this and can I disable this behaviour?

To further clarify the issue I'm having with an example: assume I have a sprite of size 20x30 (transform.scale) with a 1x1 texture. Then I want to use a different texture for the sprite so I decide to use a texture of dimensions 10x15 then all of the sudden the dimensions of the sprite are now (10*20x15*30) but I want the sprite to remain fixed at 20x30 regardless of the size of the given texture.

in Engine by (15 points)

1 Answer

+2 votes
Best answer

This is how Godot works. Units of the 2D engine are in pixels, and there are no further size conversions. So when you set a texture on a sprite, the sprite takes the same size as the texture.

If you want to undo that behavior, you can have a script modify the scale using a reference size:

scale = Vector2(20, 30) / texture.get_size()

Or you could even make a custom node in which you use _draw() to render textures at a fixed size no mattter what they are.

If you are making a GUI, you could also use the TextureRect node instead (it's a Control node so you can give it a size even without a texture), then make the expand property checked and stretch mode set to Scale.

by (28,738 points)
selected by
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 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 webmaster@godotengine.org with your username.