0
\$\begingroup\$

how can I move an object along a path "relative" to its inital position? I want it so if the object is initially positioned "around" the path instead of in the center, it will move along the path keeping that "displacement", as the below images

The blue line is the path and the red line is what the object, in this case the zombie, would follow if it had been initially positioned in the left and right sides of the "blue path", respectively: Text Text

Hope you understood. So far I've this

func move_zombie(delta):
    if target == null:
        return

    var target_pos = target.get_position()
    var pos = get_position()
    var dir = (target_pos - pos).normalized()
    var zombie_speed = 200.0
    position += delta * zombie_speed * dir
\$\endgroup\$
1
  • \$\begingroup\$ The key word here is "flanking". It's the best description of the wanted behavior. For example: reddit.com/r/gamedev/comments/4lbx2b/ai_flanking, I haven't tested it myself yet. You can answer your own question if you get good results. About not flanking when axis aligned, just compare again target's position and disable flanking. Google for more flanking strategies, but I liked that one. Or better, randomize behavior, flank 40% of the time, go straight 60% of the time. Overuse a strategy will make player resistant again it. \$\endgroup\$ Commented Jun 13, 2020 at 13:13

1 Answer 1

2
\$\begingroup\$

You'll track two separate position variables here: your current on-path position, and your current actual position, shifted from the path.

Store your offset from the start of the path when you first begin traversing:

waypoint_index = 0;
path_position = path.get_waypoint(waypoint_index);
offset = actual_position - path_position;

When you update your path position, something like this...

var next_waypoint = path.get_waypoint(waypoint_index);
var difference = next_waypoint - path
var step_size = speed * delta_time

if(dot(difference, difference) <= step_size * step_size) {
    // Snap to the waypoint instead of overshooting.
    // (You can also track how much "loose change" movement you have left
    //  and spend it advancing toward the next waypoint after this one)
    path_position = next_waypoint
    waypoint_index++
} else {
    path_position += difference.normalized * step_size
}

Then you also update your actual position by adding your offset back in:

actual_position = path_position + offset
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .