Godot Version 4.2
Question
I have a Path2D and this:
PathFollow2D
├ HitPointsBar
├ TierIndicatorSprite
├ VisibleOnScreenEnabler2D
├ OffScreenIndicatorSprite
└ MainSprite
Problem 1:
The rotation is 90deg wrong when PathFollow2D.rotates == true
.
How should this be corrected?
Problem 2:
I only want the MainSprite to rotate, the other stuff should only move, but keep their rotation at 0. Is there some way to have PathFollow2D.rotates == false
but get the moving direction in GDScript, to then use that for lerping the rotation or can you think of some other way to solve this?
This works, but I’m thinking there is a more proper way to do it
func _physics_process(delta):
progress += speed * delta
$MainSprite.rotation = rotation - PI/2
rotation = 0
I had similar issue when using PathFollow2D with rotation. I used a script to reset the rotation and afterwards add a lean depending on the direction the AnimatedSpirite2D was facing.
extends Sprite2D
@export var lean = 12
var path_rot_old = null
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
var _parent = self.get_parent()
# FIXME: Check if its a PathFollow2D or at least has property
var path_rot = self.get_parent().get_parent().rotation_degrees
var lean_override = 0
# INFO: For optimization
# quadrant I and II are same
# as are III and IV
# INFO: Only adjust if rotation did change
if path_rot != path_rot_old:
path_rot_old = path_rot
# INFO: No leaning when moving straigth in Y direction
lean_override = -path_rot
## INFO: moving in Quadrant I
if path_rot > -90 && path_rot <= -1:
lean_override += lean
# INFO: moving in Quadrant II
if path_rot >= 0 && path_rot < 90:
lean_override += lean
# INFO: moving in Quadrant III
if path_rot > 90 && path_rot <= 180:
lean_override -= lean
## INFO: moving in Quadrant IV
if path_rot >= -180 && path_rot < -90:
lean_override -= lean
_parent.rotation = deg_to_rad(lean_override)
The quadrants are taking from Math class with the first one being positive X and positive Y. Godot cords would be quadrant II.
My Node setup:
Path2D
+PathFollow2D
++AnimatableBody2D
+++ColissionShape2D
+++Sprite2D
Add the script to the Sprite or at least to the AnimatableBody.
I first had that script attached to Path2D but this created ghosts when the direction of the Path would change (also only for one frame but still noticable).
There also is the global_rotation property but I did not fiddle with this one.
You might also have to rearrange your node to only rotate one node and have the rest as childs and thus should take the rotation from its parent.