• 2D
  • How do I rotate a sprite? Help, I don't understand!

Can't rotate sprite in 2D along the (X, Y) axes. I want to rotate my sprite with (Scale) but I can't get it right.

TwsitedTwigleg note: From a merged topic

hi, in 2d you don't have a choice of axis to rotate , if you could rotate on x or y you would be in three dimensions.

scale is to change size. set_flip_h is to flip the sprite horizontally , if it was facing right it would be facing left.

I would suggest that you should try some basic tutorials, there is a ton of information out there to answer this question and the tutorials are kind of fun.

everything you have written in your code example is found in the inspector tab, you can play with that tab to see what the different options do.

self.rotation_degrees= 45

will rotate 45 degrees the wrench and screw driver in the top right hand corner has access to the documentation for the Sprite node

TwsitedTwigleg note: From a merged topic

What exactly are you trying to achieve? Can you post a screenshot of what you are looking for?

Also, is there a reason the rotation or rotation_degrees properties do not work for rotation? Are you trying to mirror your sprite (flip it on the horizontal/vertical axis)? Its hard to help without more information on what you are trying to do and what you have tried.


Also, it should be noted that this is a duplicate of an earlier thread: How do I rotate a sprite? Help, I don't understand!. Please do not make duplicate threads, instead just bump the original topic with some relevant information. Alternatively, you can bump it with something like "I still have not got this to work. Does anyone have any ideas?" or something similar.

I will merge these threads together in just a bit, but again, please avoid making duplicate threads. Thanks!

Edit: The three posts above this one are from the old, now merged, thread. I added a little note to each so it is clearer for those looking at this thread for the first time.

Can't rotate sprite in 2D along the (X, Y) axes. I want to rotate my sprite with (Scale) but I can't get it right.

TwsitedTwigleg note: From a merged topic

What exactly are you trying to achieve? Can you post a screenshot of what you are looking for?

Also, is there a reason the rotation or rotation_degrees properties do not work for rotation? Are you trying to mirror your sprite (flip it on the horizontal/vertical axis)? Its hard to help without more information on what you are trying to do and what you have tried.


Also, it should be noted that this is a duplicate of an earlier thread: How do I rotate a sprite? Help, I don't understand!. Please do not make duplicate threads, instead just bump the original topic with some relevant information. Alternatively, you can bump it with something like "I still have not got this to work. Does anyone have any ideas?" or something similar.

I will merge these threads together in just a bit, but again, please avoid making duplicate threads. Thanks!

Edit: The three posts above this one are from the old, now merged, thread. I added a little note to each so it is clearer for those looking at this thread for the first time.

Thank you very much for responding and correcting me. If specifically I need to reflect my sprite along the X or Y axes, I attach the file. P.s sorry for my english.

https://youtu.be/U3GUbiLaJxY

Ah, I see. Okay, well giving the illusion of rotation on a single axis using scale is not too hard, thankfully. The code below should achieve the effect you are looking for, assuming the Sprite node's pivot is in the center of the Sprite:

extends Sprite
export (float) var scale_speed = 1
var scale_down = true

func _process(delta):
	if (scale_down == true):
		scale.x -= delta * scale_speed
		if (scale.x <= -1):
			scale_down = false
			scale.x = -1
	else:
		scale.x += delta * scale_speed
		if (scale.x >= 1):
			scale_down = true
			scale.x = 1

The code above should work for scaling the sprite on the X axis, giving the illusion of rotation. You can get the same effect on the Y axis by doing the same thing, just modifying scale.y instead of scale.x. I have not tested the code, but I am fairly certain it should work :smile:

@TwistedTwigleg said: Ah, I see. Okay, well giving the illusion of rotation on a single axis using scale is not too hard, thankfully. The code below should achieve the effect you are looking for, assuming the Sprite node's pivot is in the center of the Sprite:

extends Sprite
export (float) var scale_speed = 1
var scale_down = true

func _process(delta):
	if (scale_down == true):
		scale.x -= delta * scale_speed
		if (scale.x <= -1):
			scale_down = false
			scale.x = -1
	else:
		scale.x += delta * scale_speed
		if (scale.x >= 1):
			scale_down = true
			scale.x = 1

The code above should work for scaling the sprite on the X axis, giving the illusion of rotation. You can get the same effect on the Y axis by doing the same thing, just modifying scale.y instead of scale.x. I have not tested the code, but I am fairly certain it should work :smile:

Thanks, this turned out to be easier than I thought. You helped me a lot! ;)

a year later

Here is an example in C#:

using System;
using Godot;

public class RotateSprite : Sprite
{	 
	[Export]
	float speed = 0.001f;
	public bool scaleDown = true;
	
	public override void _Process(float delta)
	{
		Scale = new Vector2((float)Math.Sin(OS.GetTicksMsec() * speed), Scale.y);
	}
}