Avatar Follow Avatar

I was looking around some previous coding and I could not find any thread even relating to this subject. I know I won’t be able to do this alone so I was wondering if anyone has a code already made to have an avatar follow another avatar. So let’s say

Friend’s coords (5,5,5)
Our’s (4,4,4)
He moves to
(8,8,8)
we move to
(7,7,7)
Basically just like a stalker. I am very sorry for bothering you all yet again just hoping you could share some of your work with me. Also, your name will be in credits for the final version of my game if you’d like.

I have something vaguely similar. In my game I have red ghosts (PacMan-style) who try and pursue the player, and I have a functionning pathfinding algorithm for this. However, at the moment the ghosts don’t stop when they reach the player; they run straight through, then turn around and run through the player again (stopping isn’t important, because eventually the player is supposed to die immediately if touched by a ghost). However, the ghosts only charge the player if the player is within their detection range, which I represent with a collision sphere. I’ll post the relevant code, though mind that the indentation is off, because the code is within a class’ method.

First, this is the collision sphere:

        #This is the collision sphere that will detect the player,
        #and help the ghost run away if the player gets too close.
        self.hunterHunt = self.hunter.attachNewNode(CollisionNode("hunterHunt"))
        self.hunterHunt.node().addSolid(CollisionSphere(0, 0, 0, 25))
        #Here we enable both From and Into collision masks on the ghosts, because, on the one hand,
        #they have to be stopped by walls; on the other hand, they also have to be collided into
        #by blasts, which are meant to destroy them.
        self.hunterHunt.node().setFromCollideMask(world.huntMask)
        self.hunterHunt.node().setIntoCollideMask(BitMask32.allOff())

[...]

        self.hunterFleeing = CollisionHandlerQueue()

        base.cTrav.addCollider(self.hunterHunt, self.hunterFleeing)

This is the code, in a separate Move task that is constantly run, which makes the ghost turn to face the player. This will probably be of the most use to you. I use trigonometry to calculate the heading from the ghost to the player, then give the ghost that heading.

        if self.isHunting == False:
            for i in range(self.hunterFleeing.getNumEntries()):
                entry = self.hunterFleeing.getEntry(i)
                if (entry.getIntoNode().getName() == "pacmanHunt"):
                    self.isHunting = True
                    self.pacPos = entry.getSurfacePoint(self.hunter)
                    self.ghostPos = self.hunter.getPos(self.hunter)
                    self.huntVec = self.pacPos - self.ghostPos
                    self.huntHeading = math.degrees(math.atan2(self.huntVec.getY(), self.huntVec.getX())) + 90
                    if self.huntHeading < 0:
                        self.huntHeading += 360
                    self.hunter.setH(self.hunter, self.huntHeading)
                    taskMgr.doMethodLater(0.5, self.keepHunting, "keepHunting")

This is the “keep hunting” task, which reenables the pursuing behavior after a half second.

    def keepHunting(self, task):
        self.isHunting = False

And then I use this line in the task to move the ghost forwards:

            self.hunter.setY(self.hunter, -10 * globalClock.getDt())

There are a lot of other elements in the method for movement, such as randomly changing directions every 5 seconds, and avoiding walls, but I figure that this is most interesting for you. If you want, I can just post the entire class, but a lot of the code might be tailored to the specific game I have in mind, and might not be suitable to your needs.

I hope this helps!

I didn’t use it yet but pandAI does have a persuie and escape features.

That you very much. I am just trying to think… Because changing from a npc following someone to an avatar following another avatar is a big change… correct? Hmmm… Thank you I will see what I can do with your code. If anyone else could post examples as well the more the merrier.

I haven’t used this yet, but I’ve been drawing up concepts for my own game.
I was going to lock onto a target using the CompassEffect. If the distance between the follower and the target is greater than X run toward the target.

It’s just pseudo-code, but maybe it will help as far as ideas go.

I’m not that good coding lol I probably wouldn’t be able to do that by myself :frowning: thanks very much for the suggestion though.