4
\$\begingroup\$

What's a solid way to have a character stuck to another one?

I have a moving character (say followed) and I want a second one (say follower) to follow the first without chance of getting stuck into obstacles.

I thought that I could save every delta velocity vector of the followed and having the follower consuming one each frame after it reached its target's starting position.

More to say, dynamic obstacles could be an issue.

Since this sounds to me like a common topic (despite the fact that I'm having difficulties in finding answers), is there anyone who could enlighten me on a proper way of facing the issue?

Edit:

Since I'd like to describe something more like to an edge case of a pathfinding algorithm, consider the following:

  • follower should always be at a fixed position related to followed
  • follower must not necessarily interact with the environment the way followed does
\$\endgroup\$
2
  • \$\begingroup\$ If I understand this correctly, you're not looking for the follower to retrace the followed character's path, rather you want them to always be at a fixed offset? If so, what's preventing you from just treating them as one object? \$\endgroup\$ Commented Sep 26, 2019 at 11:11
  • \$\begingroup\$ @RutherRendommeleigh that could make sense but has a few issues. depending on the engine, you could have to override several behaviors like mirroring position updates, rotations, and so on. \$\endgroup\$
    – Leggy7
    Commented Sep 26, 2019 at 13:27

2 Answers 2

3
\$\begingroup\$

It sounds like you want one object to be "towed" behind the other, as though it were tethered to it by a free-swinging, rigid bar of a fixed length, and ignoring any obstacles along the way.

Instead of pathfinding around obstacles, or copying the leader's path exactly, this object will dead-reckon toward the leader, cutting corners when the leader turns, to follow the shortest path that maintains its fixed following distance. If the leader starts moving toward the follower, the follower will back up as though pushed by this tow rod, until the bar is nudged far enough to the left or right that it can swing around the leader again and resume the chase from the other side.

If that description matches your desired behaviour, then we can do this with a very simple trick:

// Get the vector pointing from the leader to the follower.
Vector offset = follower.position - leader.position;

// Scale this vector so that it's exactly as long as our desired distance.
offset = offset * followDistance / Length(offset);

// Snap the follower to the leader's position, shifted by this offset.
follower.position = leader.position + offset;

If you want to apply physics-based movement, then instead of setting the position directly on the last step here, you can use the desired change in position to compute a velocity that will bring the object there in the next time step. This will play nicer with collisions, if the follower is meant to push objects or be pushed.

\$\endgroup\$
0
\$\begingroup\$

It's pathfinding to a dynamic target. Every few frames you repath to find the new best path to the player.

doing frequent repaths to a close by target means that dynamic obstacles aren't that much of an issue.

Of course there is always the old cheat: teleport the character when it's offscreen instead of making it walk.

\$\endgroup\$
2
  • \$\begingroup\$ I thought that having a character stuck to another could have been considered an edge case, thus normally treated differently, simplier possibly. Let me edit the question to describe better the simple case study. I'll try to be as clear as possible \$\endgroup\$
    – Leggy7
    Commented Sep 26, 2019 at 10:10
  • \$\begingroup\$ The answer is correct, but you still need to compute an offset so that the characters don't overlap. \$\endgroup\$
    – Șerban
    Commented Oct 1, 2019 at 8:59

You must log in to answer this question.

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