Hi there !
So , since there’s no way to build NPC natively , i build an Abstract Control class to auto hover(on y-axis) NPC on the water surface & towards the user (on z-axis) & added the control to the NPC spatial
Code:
package Mars_Map;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
/**
*
* @author twisted
*/
public class NPCAutoControl extends AbstractControl {
private CharacterControl player;
private CharacterControl NPC_control;
private Camera cam;
private Vector3f NPCmotion=new Vector3f();
private float time=0.0f;
private float zDir=0f;
private float xDir=0.0f;
public NPCAutoControl(CharacterControl player, CharacterControl NPC_control,Camera cam) {
this.player=player;
this.NPC_control=NPC_control;
this.cam=cam;
/**
* getting the start position of the NPC's
*/
NPCmotion=NPC_control.getPhysicsLocation();
}
@Override
protected void controlUpdate(float tpf) {
/**
* Create CoolDown motion of around 0.3f frames
*/
time+=tpf;
if(time >=0.25f){
if(player.getPhysicsLocation().distance(NPC_control.getPhysicsLocation()) <= 200 ){
hoverNPC_Up();
moveNPC();
attackNPC();
}else{
NPC_control.setWalkDirection(new Vector3f(0,0,0));
NPC_control.setPhysicsLocation(new Vector3f(72.81782f,-60f,760.87f));
}
System.out.println(NPC_control.getPhysicsLocation());
/**
* Reset CoolDown motion
*/
time=0.0f;
}
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
private void moveNPC() {
/**
* Move the enemy Rover towards the player Rover
*/
if(player.getPhysicsLocation().distance(NPC_control.getPhysicsLocation()) >= (200f/2f) ){
/**
* steppping NPC's motion
*/
NPCmotion.set(0,0,0);
/**
* adding values on Z axis towards player camera direction
* Note : negation on z-axis is done to ensure the NPC moves against the camera Direction
*/
zDir=-cam.getDirection().getZ();
NPCmotion.addLocal(new Vector3f(0f,0f,zDir));
/**
* setting the driection & the vaules of NPC motion
*/
NPC_control.setWalkDirection(NPCmotion);
}else{
/**
* Reset the NPC motion & freezes it in place ..if the distance is less than 100f
*/
NPCmotion.set(0,0,0);
NPC_control.setWalkDirection(NPCmotion);
}
}
private void hoverNPC_Up() {
/**
* Rise the Enemy Rover up on the water Surface
*/
if(NPC_control.getPhysicsLocation().getY() <= -10f){
/**
* Jump the NPC on the y-axis by 30f world units
*/
NPC_control.jump();
/**
* Change the looking view of NPC camera to lookAtThePlayer
*/
NPC_control.setViewDirection(cam.getDirection());
NPC_control.setEnabled(true);
}
}
private void attackNPC() {
}
}
Now the problem lies in this snippet :
private void moveNPC() {
/**
* Move the enemy Rover towards the player Rover
*/
if(player.getPhysicsLocation().distance(NPC_control.getPhysicsLocation()) >= (200f/2f) ){
/**
* steppping NPC's motion
*/
NPCmotion.set(0,0,0);
/**
* adding values on Z axis towards player camera direction
* Note : negation on z-axis is done to ensure the NPC moves against the camera Direction
*/
zDir=-cam.getDirection().getZ();
NPCmotion.addLocal(new Vector3f(0f,0f,zDir));
/**
* setting the driection & the vaules of NPC motion
*/
NPC_control.setWalkDirection(NPCmotion);
}else{
/**
* Reset the NPC motion & freezes it in place ..if the distance is less than 100f
*/
NPCmotion.set(0,0,0);
NPC_control.setWalkDirection(NPCmotion);
}
}
these lines specifically:
NPCmotion.set(0,0,0);
/**
* adding values on Z axis towards player camera direction
* Note : negation on z-axis is done to ensure the NPC moves against the camera Direction
*/
zDir=-cam.getDirection().getZ();
NPCmotion.addLocal(new Vector3f(0f,0f,zDir));
/**
* setting the driection & the vaules of NPC motion
*/
NPC_control.setWalkDirection(NPCmotion);
I cannot accurately move the NPC towards the player by some physical units …it sometimes works when i am pointing the camera to the +ve z-axis direction otherwise it moves around in-accurately !