Get the direction a node is going from PathFollow2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 1izNoob

I have a (pixelart) game where I want a rigidbody to follow a Path2D.

So my setup is this:

On the level scene:

  • PathFollow2D

And the Pathfollow2D just gets

pos.offset += 50 * delta

Then I have another scene with a rigidbody with the following code:

onready var path_position = get_node("/root").find_node("PathFollow2D",true,false)

func _process(delta):
    position = path_position.position

All of this works well and the rigidbody follows the path. But now I am stuck on how to change the AnimatedSprite of the RigidBody when it is moving in different directions.

I tried to use the linear_velocity property of the rigidbody, but that one only gives me a Vector2 where the y value increases indefinitely and the x value remains at 0 (which I don’t understand).

Can someone help?

:bust_in_silhouette: Reply From: Magso

linear_velocity is the directional speed of a rigidbody according to the physics engine i.e. when the rigidbody is free falling. However this will just return 0,0 if the position of the rigidbody is being set directly like PathFollow’s offset does. In this case a Vector2 variable needs to be made to compare the current position to the last position.

func _process(delta):
    var old_pos = position
    yield(get_tree(),"idle_frame") #allow for pos.offset += 50 * delta
    var direction = position - old_pos

Thank you so much, that is really helpful.

I am a little confused when trying to use it though, hope you can help:
If I use position (like in your code) the y numbers on direction Vector seem to increase exponentially and reach >10000 after just a few seconds. The x numbers seem to be doing okay though.

I assumed that was some kind of problem with the parenting so I used global_position instead of position (both for old_pos and for direction), and for this one I always get 0 and it seems that Godot ignores the idle_frame. When I print both individually I always get the same position for the old_position and for the position after the idle frame.

Also, when the node is moving upwards in a vertical line, I still get x values that are > 0, although that only happens when it is moving upward, moving downward gets me x = 0. I am quite confused by it, would be amazing if you could help!

Thank you so much in advance :slight_smile:

1izNoob | 2020-02-22 16:16

That’s peculiar since taking away the old position should equal the difference and shouldn’t constantly increase.

Do you still have this code running?

func _process(delta):
    position = path_position.position

if this is for the rigidbody it will keep trying to set the rigidbody’s position to the path’s node position which may be causing the issue.

If this isn’t the issue it might be worth getting the direction of the rigidbody in the path node’s script.

var rigidbody #the rigidbody reference
func _process(delta):
    var old_pos = rigidbody.position
    get_child(0).offset += 50 * delta
    var direction = rigidbody.position - old_pos

Magso | 2020-02-22 17:10

I am really sorry but I am a bit struggling to follow:

func _process(delta):
position = path_position.position

Yes, that code is still in place. But I am not sure how setting that position to the path’s node position causes problems. Isn’t that exactly what the goal is?

var rigidbody #the rigidbody reference
func _process(delta):
    var old_pos = rigidbody.position
    get_child(0).offset += 50 * delta
    var direction = rigidbody.position - old_pos

Could you explain what a rigidbody reference is, or link a tutorial? I’ve never seen something like that before and can’t find any tutorial to explain it.

Thank you so much again!

1izNoob | 2020-02-23 10:12

The PathFollow2D consists of a local origin point and the path points. Assuming the the PathFollow2D node is at 0,0, position = path_position.position will set the rigidbody’s local position to 0,0. The offset property is what interpolates the rigidbody around the path points.

A rigidbody reference is assigning a rigidbody node to a variable like this.

onready var rigidbody = get_node("path/to/rigidbody")

Magso | 2020-02-23 13:33