Player can not strafe left or right, only walk forsward and backward

Hello Monkeys! :wink:

I’m programming a game (of course) and ran it to some problem. I finally made my character Oto ( as your probably know) able to walk!

My problems are:

  1. Oto can not strafe left or right, only walk forward and backwards and jump.

  2. I’m using a chase cam but I need to make a first person camera and don’t know how to do.

  3. What is the difference between charactercontrol and bettercharactercontrol?

  4. Where i declare player = new charactercontrol, there is a blackline over “chactercontrol”, why? :stuck_out_tongue:

I’ve checked multiple tutorials but not really found what i’m looking for. Here is the relevant code:

[java]
public class Main extends SimpleApplication implements ActionListener {

private CharacterControl player;
private Spatial sceneModel;
private Vector3f walkDirection = new Vector3f();
private RigidBodyControl landscape;
private BulletAppState bulletAppState;
private boolean left=false,right=false,up=false,down=false;

Node playerModel;
ChaseCamera chaseCam;

private Vector3f camDir;
private Vector3f camLeft;

public static void main(String[] args) {
    Main app = new Main();
    app.start();
}

@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);

    sceneModel = assetManager.loadModel(“Scenes/sceneModel.j3o”);
    CollisionShape sceneShape =
        CollisionShapeFactory.createMeshShape((Node) sceneModel);
    landscape = new RigidBodyControl(sceneShape, 0);
    sceneModel.addControl(landscape);

    initKeys();
    createOto();
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());
    rootNode.addLight(dl);

    rootNode.attachChild(sceneModel);
    bulletAppState.getPhysicsSpace().add(landscape);
    bulletAppState.getPhysicsSpace().add(player);
}

private void createOto(){
    flyCam.setEnabled(false);

    playerModel = (Node) assetManager.loadModel(“Models/Oto/Oto.mesh.xml”);
    playerModel.setLocalScale(1.0f);
    playerModel.setLocalTranslation(new Vector3f(1, 10, 1));

    chaseCam = new ChaseCamera(cam, playerModel, inputManager);
    chaseCam.setDefaultDistance(37);

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

    playerModel.addControl(player);

    rootNode.attachChild(playerModel);
}

@Override
public void simpleUpdate(float tpf) {
    camDir = new Vector3f();
    camLeft = new Vector3f();
    camDir = cam.getDirection().clone().multLocal(0.6f);
    camLeft = cam.getLeft().clone().multLocal(0.4f);
    walkDirection.set(0, 0, 0);

    if (left){
        walkDirection.addLocal(camLeft);
    }
    if (right){
        walkDirection.addLocal(camLeft.negate());
    }
    if (up){
        walkDirection.addLocal(camDir);
    }
    if (down){
        walkDirection.addLocal(camDir.negate());
    }
    player.setWalkDirection(walkDirection);
    cam.setLocation(player.getPhysicsLocation());

}

@Override
public void simpleRender(RenderManager rm) {
    //TODO: add render code
}

private void initKeys(){
    inputManager.addMapping(“Left”,new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
    inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addListener(this, “Left”);
    inputManager.addListener(this, “Right”);
    inputManager.addListener(this, “Up”);
    inputManager.addListener(this, “Down”);
    inputManager.addListener(this, “Jump”);
}

public void onAction(String name, boolean isPressed, float tpf) {
    if (name.equals(“Strafe Left”)){
        if (isPressed){
            left = true;
        }
        else {
            left = false;
        }
    }
    else if (name.equals(“Strafe Right”)){
        if (isPressed){
            right = true;
        }
        else{
            right = false;
        }
    }
    else if (name.equals(“Up”)){
        if (isPressed){
            up = true;
        }
        else{
            up = false;
        }
    }
    else if (name.equals(“Down”)){
        if (isPressed){
            down = true;
        }
        else{
            down = false;
        }
    }
    else if (name.equals(“Jump”)){
        if (isPressed){
            player.jump();
        }
    }

}

}
[/java]

You are setting up your mapping for left and right as “Left” and “Right”… not “Strafe Left” and “Strafe Right”… they need to match. :slight_smile:

1 Like
@monkeylover said: You are setting up your mapping for left and right as "Left" and "Right"... not "Strafe Left" and "Strafe Right"... they need to match. :)

Thank you man you are a king! :slight_smile:

1 Like
    • I quite don’t know how to do that. But there are lots of topics of people needing to create a first person camera. Go google it!
  1. The BetterCharacterControl is a new version of CharacterControl.

  2. There is a blackline because the CharacterControl is deprecated. When a class or method is deprecated, usually the javadoc says what class or method to use instead. In this case, use BetterCharacterControl. Don’t use deprecated classes: there’s always a reason to deprecate a class.

1 Like
@Ev1lbl0w said: 2. - I quite don't know how to do that. But there are lots of topics of people needing to create a first person camera. Go google it!
  1. The BetterCharacterControl is a new version of CharacterControl.

  2. There is a blackline because the CharacterControl is deprecated. When a class or method is deprecated, usually the javadoc says what class or method to use instead. In this case, use BetterCharacterControl. Don’t use deprecated classes: there’s always a reason to deprecate a class.

Thanks for answering! I made the using flycam as first person from hello collision tutorial and it works fine! :slight_smile:

Ok I will change to BetterCharacter instead then! :slight_smile: