Path like movement without path

Godot Version

4.2.2

Question

Hi Community,
i stuck on this now for about 4 weeks. My Brains stops working and i need help. There is an array of Vectors coming from a AStar. How to move along them is clear. Can make this with velocity, set the position directly or create a path to move along. But the object should move like along a path but without a path. Better to explain this with the following image:

From point to point without a path. I also have tried to make it with bezier(via lerp). But then the speed is different cause the distances are not equal. A corner and a diagonal movement got not the same length. I can feel that i overlooked something easy to solve this (hope so :wink: ). Like i wrote my brain has dead end now. Pleeease can somebody help me out on this?

I dont quite understand what you are trying to achieve. Why dont you want a path, its exactly what you are looking for

What is that supposed to mean?

Hello herrspaten, thanks for the reply and sorry for the poor explanation. Like i said my brain is broken :wink: There are two Probs with this.

  • First is that it is a character that is walking. So with a path i have to set the position with 0.0 - 1.0. But the paths are changing. So if he reaches the target-point and i click on another one there is a new AStar-path-array with another length. So how could i ensure to ever walk in the same speed?
  • Second is that there could be moving obstacles in the way like balls, crates or even npcs.
1 Like

This sounds like you should use the navigationsystem.
If you want to do it without it you have to do something like this:

var path_points: PackedVector2Array
var path_index: int = 0
var target_reached: bool = true
var current_point: Vector2

func add_new_path(new_path_points: PackedVector2Array) -> void:
    target_reached = false
    path_index = 0
    path_points = new_path_points
    current_point = path_points[path_index]

func get_next_path_point() -> void:
    path_index += 1
    if path_index > path_points.size():
        print("Target Position reached")
        target_reached = true
        return

    current_point = path_points[path_index]

func _physics_process(delta: float) -> void:
    if not target_reached:
        follow_path()


func follow_path() -> void:
    var distance: Vector2 = current_point - global_position
    if distance < TARGET_REACHED_THRESHOLD:
        get_next_path_point()
        if target_reached():
            return
        distance = current_point - global_position
    var direction: Vector2 = distance.normalized()
    
    velocity = direction * speed
    move_and_slide()


Thank you, but with this code the character do not walk that bezier curves. It do hard turns. Maybe i have it wrong implemented?

BTW i don’t told you that it is 3d and not 2d but that is not so wild. I rewritten it to 3dVectors.

No i didnt know you wanted bezier-curves. Can you show your implementation with lerp? It should be possible todo with constant speed

That would be great. I will post a cleaned code tomorrow. Now it is a mess cause of all the experiments :smiley: i can’t take more today. Thank you so much.

1 Like

OMG i have done it. Good sleep and the communication was all i needed. The solution was to compare the distances between the points. If it is more then the straight connection, the time value on the bezier must be corrected. Thats all. Hope the image explains it better than the last one.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.