Is it possible for a RigidBody2D that is set on rails by a path2D,to have its pattern altered by the physics engine?

Godot Version

4.5

Question

wanted to test an idea,where enemies set on rails would colide with a wall, and have their sprite animations and movment pattern altered by the collision,like this:
pattern example

You could use the PathFollow2D as a target position, then use physics such as apply_central_force towards that target position, increasing the PathFollow2D progress as the RigidBody draws near. This however would fail if the next position is perpendicular to the wall and completely obstructed, like any other naive follow.

1 Like

You could try skewing the path in real time, but that might have some weird effects.

1 Like

hello,thanks for the tips,i’m struggling with the implementation though, tried searching on how to use move_and_slide and pyshics_process but can’t make it work inside the path2D or call my character2D that is instantiated with a rigidbody,it is returning null,tried with signals too but could’t make it work either

The character or rigid body wouldn’t be a child of the Path2D or the PathFollow2D, they would use maybe an @export variable to reference the PathFollow2D and try to move towards it, like any other follow script.

@export var target_node: Node2D # assign this to your PathFollow2D

const speed: float = 200
const acceleration: float = 1000

func _physics_process(delta: float) -> void:
    var difference := target_node.global_position - global_position
    var direction := difference.limit_length(1.0)

    velocity = velocity.move_toward(direction * speed, acceleration * delta)
    move_and_slide()