Make your node rotate – Godot

Leggi in Italiano

This is how you can make your Node2d rotate in godot.
Okay this is a very basic thing to do, begginer stuff. You can watch the video, it’s fast and usefull. If you want more info, read this post.
Rotation is a deafult property in nearly every node in godot engine.

Let’s give a look to the inspector after selecting a Node2D. There are different groups here: Transform, Z Index, Visibility, Material.
Give a look to “Transform” , you can change values of the position, scale and Rotation Degrees.

Rotation_Degrees is the property we will change.


extends Node2D
#Godot v3.5.1

func _physics_process(delta):
	#change angle in degrees
	rotation_degrees = 0
	#change angle in radiants
	rotation = 0
	#change angle in radiants 
	set_rotation(0)
	#change angle in degrees
	set_rotation_degrees(0)
	#make node rotate with degrees
	rotation_degrees += 1
	#make node rotate with radiants
	rotation += 0.01
	#make node rotate, sets node angle to the actual node angle "get_rotation()" adding 1
	set_rotation(get_rotation()+1)
	#make node rotate, sets node angle to the actual node angle "get_rotation_degrees()" adding 1
	set_rotation_degrees(get_rotation_degrees()+1)

The code in the upper box is gdscript, I’m using python synthax since there’s no gdscript sythax aviable

You can access in different ways to the rotation property. You can give a look to the code box up. As can be seen there are different ways to set the angle of the node.
You can use degrees or radiants. You can access to the property by using keywords to call the property.

  • rotation this allows you to set the angle of the node by using radiants
  • rotation_degrees this allows you to set the angle of the node by using degrees

Alternatively you can use set_rotation and set_rotation_degrees.

  • set_rotation(value) this is the same like rotation, needs a value
  • set_rotation_degrees(value) this is the same like rotation degrees, needs a value

Problems

A problem you may have is that your game may act differently according to the macchine the game runs in, especially old machines or smartphones. For example, the rotation may result slower or faster according to the hardware.
In order to have the same speed of rotation in every macchine you need to multiply by delta.

extends Node2D
#Godot v3.5.1

func _physics_process(delta):
	#change angle in degrees multiplied by delta
	rotation_degrees += 1*delta

The code in the box up above works okay but results slow. To fix it you can use a variable to change the speed.

extends Node2D
#Godot v3.5.1
var Speed = 100

func _physics_process(delta):
	#change angle in degrees multiplied by delta and by speed
	rotation_degrees += 1*delta*Speed

In conclusion, this is how you can make a Node2D rotate always like in the video.
At this point you can try yourself experimenting and tweeking all the properties.

,

One response to “Make your node rotate – Godot”