Got an NPC to follow a Player, but needs some tweaks

I used ThirdPersonCamera, thanks to Berzee:Third Person Camera Test (jMonkeyEngine 3) - YouTube

So i have a class called Ogre that extends Node:

[java]
walkDirection.set(game.getCamera().getCharacterControl().getViewDirection());
ogre.setWalkDirection(walkDirection.normalize().multLocal(movementSpeed).setY(0f));
[/java]

So if enter a close distance to the ogre, he starts walking towards me however he goes wherever the camera is. If the camera is behind the player it looks like it works fine but if its to sides or front he walks towards that direction. I know i could just make the game first person and it works(i tested) but i would prefer 3rd person.

I tried setting the ogres walk direction to players model with local and world translation and that does not work that good as view direction. How can i get the ogre to walk towards the playerModel(Spatial) and not the camera node behind displaying 3rd person.

The direction from one spatial to another is easily computed using vector arithmetic. For instance:
[java]
Vector3f playerLocation = playerNode.getWorldTranslation();
Vector3f ogreLocation = ogre.getWorldTranslation();
Vector3f offset = player.subtract(ogre); // offset from ogre to player
Vector3f direction = offset.normalize(); // direction from ogre to player
Vector3f velocity = direction.mult(ogreMovementSpeed);
Vector3f horizonalVelocity = velocity.clone().setY(0f);
[/java]
I hope that helps!

In case you weren’t aware, subclassing the Node class is discouraged.

1 Like
@sgold said: The direction from one spatial to another is easily computed using vector arithmetic. For instance: [java] Vector3f playerLocation = playerNode.getWorldTranslation(); Vector3f ogreLocation = ogre.getWorldTranslation(); Vector3f offset = player.subtract(ogre); // offset from ogre to player Vector3f direction = offset.normalize(); // direction from ogre to player Vector3f velocity = direction.mult(ogreMovementSpeed); Vector3f horizonalVelocity = velocity.clone().setY(0f); [/java] I hope that helps!

In case you weren’t aware, subclassing the Node class is discouraged.

Thank you so much! You have really helped in a lot of my posts!

2 Likes