Hello again. I know how to move an object in a 2D world using buttons, but I can't figure out how to make an object move forward depending on its rotation.
I attach a photo from below. Thanks for your attention.
Hello again. I know how to move an object in a 2D world using buttons, but I can't figure out how to make an object move forward depending on its rotation.
I attach a photo from below. Thanks for your attention.
You just need to convert the rotation to a vector using cos
and sin
. You may also need to apply an additional offset, depending on where "forward" is relative the node's rotation. Here's a code snippet (untested):
extends Node2D
func _process(delta):
var forward_dir = Vector2(cos(rotation), sin(rotation))
# then you can move the node by using it as a direction vector
translation += forward_dir * delta * 100
# you can also use global_rotation instead of rotation, which is helpful
# if the Node2Ds parent also rotates.
Hello, friend! Thanks for the answer. It works, but not correctly. The object should move up, but it moves to the right. I tried to correct the cosine and sine, but it didn't work(((. Thanks for your attention.
No problem, just add 90 or 270 degrees to the rotation, and then it should go either up/down, depending on which one is used:
var forward_dir = Vector2(cos(rotation + deg2rad(90)), sin(rotation = deg2rad(90))
# or
var forward_dir = Vector2(cos(rotation + deg2rad(270)), sin(rotation = deg2rad(270))
Ahah. Thank you very much, I did so (addition), but without this function)). Thanks again, good weekend!