How to make NPC attack the player

Im trying to develop FPS game where monsters come at me and attack me.

How i create the player(which is 1st person, so a camera)

playerNode = new Node(“Player”);

    CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
    player = new CharacterControl(capsuleShape, 0.05f);
    player.setJumpSpeed(20);
    player.setFallSpeed(40);
    player.setGravity(40);
    playerNode.addControl(player);


    player.setPhysicsLocation(new Vector3f(-150, -10, -20));


    bulletAppState.getPhysicsSpace().add(player);
    rootNode.attachChild(playerNode);

How i create a NPC

Node monster = (Node) assetManager.loadModel(“Models/Oto/Oto.mesh.xml”);
monster.setLocalScale(0.7f);
monster.setLocalTranslation(-120, -20, 0);
monster.setUserData(“Id”, id);
monster.setUserData(“Hp”, 100);
monster.setName(“Monster” + id);
rootNode.attachChild(monster);
control = monster.getControl(AnimControl.class);
control.addListener(this);
channel = control.createChannel();
channel.setAnim(“Walk”);
channel.setLoopMode(LoopMode.Loop);

monster_phys = new CharacterControl(capsuleShape, 0.01f);
// Attach physical properties to model and PhysicsSpace
monster_phys.setJumpSpeed(20f);
monster_phys.setMaxSlope(1.5f);
monster_phys.setPhysicsLocation(new Vector3f(10, 5, 0));
monster.addControl(myCharacter_phys);
bulletAppState.getPhysicsSpace().add(monster_phys);

I know that their cant be any collison but i just want the monsters to walk around normal.
Now here is my question, how am i supposed to make this NPC come to attack me?

I’ve tried all the possible things with monster_phys.setWalkDirection and nothing worked.
I just want the NPC to come to my location.

Can anyone please help me, i’ve searched the whole forum and didnt find the right solution.

Maybe try something like Garrys Mod or another FPS mod, its much easier making models you just loaded “attack” something else there.

Yeah, i was thinking about using some other mod, but i would really like to continue by just using jMonkey.
So you wouldn’t know how to implement that?

I never seem to guess how to setWalkDirection of a NPC to come in my direction.

No, I don’t know how to implement that, I guess it has something to do with vector math or the stuff that MonkeyZone does, who ever wrote that…

Get the Vector location of the player and the Vector location of the NPC, substract them and do setWalkDirection with that value. Here is an example:

[java]
public void update(float tpf)
{
Vector3f playerLocation = player.getWorldTranslation();
Vector3f npcLocation = npc.getWorldTranslation();
Vector3f offset = player.subtract(npcLocation); // offset from npc to player
npc.setWalkDirection(offset).normalize();

}
[/java]

This should get the npc to walk towards you.

Then if you want the NPC to attack you, check the distance and if less than X do an attack animation.

[java]
public void update(float tpf)
{
Vector3f playerLocation = player.getWorldTranslation();
Vector3f npcLocation = npc.getWorldTranslation();
Vector3f offset = player.subtract(npcLocation); // offset from npc to player
npc.setWalkDirection(offset).normalize();

}
[/java]
.

Thanks that worked, you just saved 10 hours of my life :smiley:

2 Likes