How to add following a character functionality to a character

how can we use motion paths to add locations to follow a person??

Using motion paths for this would be a very naive implementation but the answer to your question would be “by placing the motion path points on the path that the person takes”.

do we have to trace the path of player

If you want to make a motion path then obviously you’ll need some coordinates, yes. However as I said using a motion path doesn’t seem like the right solution here. Just make the object move towards the player.

thanks man really appericiate it
r

and i guess we have to add some animation if the player goes to a place not easily accessiblle right

to the player who is following (edit)

If You still don’t know, on my project we toke the position of the object behind folowed and we gave the same code as walking character but instead of moving from the arrow input we gave it a vector of the object folowed!

Here is the code so you can understand it easier! (We use walking character on both side)

public class NpcEnnemie extends Entity implements InterfaceController{

    @Override
    public void update(Main app, ServiceContainer service, float tpf){
        walkDirection.set(0,0,0);
        
        Vector3f posP = player.getPosition();
        Vector3f posE = ennemieCharacter.getPhysicsLocation();
        
        float x = posP.x - posE.x; //we get the relative value
        float z = posP.z - posE.z; //we get the relative value

        //at this part i was lazy, it should be the hypotenuse but i didn't done the whole math haha...
        float maxMove = FastMath.abs(x) + FastMath.abs(z);
        
        //This part is to stop once it reach a certain distance if(he can move more then 5 unit then move)
        if(maxMove > 5){
            walkDirection.addLocal(x/maxMove, 0, z/maxMove);
        }
        
        RotatePlayer rot = new RotatePlayer();//custum made class to lookAt();
        ennemieCharacter.setViewDirection(rot.lookAtPlayer(ennemieCharacter.getPhysicsLocation(), player.getPosition()));
        ennemieCharacter.setWalkDirection(walkDirection);
        
        //Ici
        setPosition(ennemieCharacter.getPhysicsLocation());
        setRotation(model.getLocalRotation());
        updateHealth();
    }
}

normen could you please elaborate more on this point, I use it in my case that involve character follower, it handles the movement and rotation just fine, is there a performance pitfalls in this approach?

Motion path is to create a path, not to follow someone. Imagine that you follow someone that make a u-turn, then you will have to go to the place where he as turned back. That’s not following, only doing the same motion.

To follow, just move in the right direction and correct that direction at each step.

But this way, you will have issues to pass corners or find your way into a maze if you are far behind, then you will want to implement pathfinding. It’s less naive ^^

1 Like

Exactly motion path save me from this, also :smile:, even though I could still store the waypoints, and move my character as per them, but it will be like implementing my own motion path, and I am sure jme3 implementation for it will be much better

I was thinking of hansel and gretel tactic to make my character follow the main player without a need to use AI, at least till now :smile:

Just a hint (fallout 4 style)

If the player is not looking, you can always just teleport a few meters behind him (just out of frustrum)
That way if you move, and he gets stuck, he will unstuck.

(Also you only need a barely working follow algorithm then, as it does not have to be stuck proove then.