getting_started/step_by_step/scripting_first_script #52
Replies: 5 comments 4 replies
-
It works but my animation was choppy at times. Disabling VSync worked for me, see: https://docs.godotengine.org/en/stable/tutorials/rendering/jitter_stutter.html |
Beta Was this translation helpful? Give feedback.
-
Hi, for the Hello World part in C#, it didn't work for me until I call GD.Print in a _Ready method. |
Beta Was this translation helpful? Give feedback.
-
I personally found that sprite2d.gd script to be a bit unintuitive and it could use some changes. It might be due to my physics/math background. I've written a separate version of this script to separate the rotation (the image turning upside down) from the circular motion (motion of the centre of the axis around some central point). extends Sprite2D
# circular motion angular speed
var angular_speed = PI/2
# rotation angular speed
var rotate_speed = 2*PI
# radius of rotation
var radius = 100
# Time vector
var mytime = 0
func _process(delta):
# Time marches on
mytime += delta
# x (left/right) and y (up/down) speed
var xvel = -radius * sin(2*PI*mytime/angular_speed)
var yvel = radius * cos(2*PI*mytime/angular_speed)
# Velocity vector
var vel = xvel * Vector2.RIGHT + yvel * Vector2.UP
position += vel * delta
rotation += rotate_speed * delta This new version of the function allows you to set the motion dimensions more directly. The radius of the circular motion and the rotation and circular motion speeds separately. I suspect there is a better way to do this in godot. I simply evaluated the derivative of the position vector of an object in circular motion my hand and hard coded it. Something I think is missing from this doc (not sure if this qualifies for an issue to be raised) is an explanation of how sprite2d.gd works to rotate the sprite. Something like:
I tried to decouple the rotation and circulation motion as follows by defining two angular_speed variables. This worked however, the radius still changes with angular_speed2 which makes sense physically but can be unintuitive. Thankfully, that only matters for anyone reading the code and not the player. extends Sprite2D
var speed = 400
var angular_speed1 = PI
var angular_speed2 = 2*PI
var myrotation = 0
func _init():
myrotation = rotation
func _process(delta):
rotation += angular_speed1 * delta
myrotation += angular_speed2 * delta
var velocity = Vector2.UP.rotated(myrotation) * speed
position += velocity * delta |
Beta Was this translation helpful? Give feedback.
-
If you are new to C# programming, the Hello World code example public MySprite2D()
{
GD.Print("Hello, world!");
} is placed in a constructor. Don't put it in the class body 'cause it won't work. |
Beta Was this translation helpful? Give feedback.
-
Hi, I had an issue with the icon displaying. I was just running through this on Godot 4.3 and my Sprite2D was not set to be visible by default. Under the Inspector tab I had to go down to Visibility under CanvasItem and toggle visibility to on before I could see the Godot icon in the scene. |
Beta Was this translation helpful? Give feedback.
-
getting_started/step_by_step/scripting_first_script
In this lesson, you will code your first script to make the Godot icon turn in circles using GDScript. As we mentioned in the introduction, we assume you have programming foundations. The equivalen...
https://docs.godotengine.org/en/stable/getting_started/step_by_step/scripting_first_script.html
Beta Was this translation helpful? Give feedback.
All reactions