I setup movement by tutorial, vec forming well, but not applying force, and i just stand, no move, how to fix?

I checked from dev tools, in source tab, my vec3 movement calculating normally, but cannot to move, what i’m doing wrong?
Editor Link PlayCanvas | HTML5 Game Engine
Script on Player Entity
Tutorial i followed by First Person Movement | Learn PlayCanvas

My script

var PlayerController = pc.createScript('playerController');

PlayerController.attributes.add('power', { type: "number", default: 1 });
PlayerController.attributes.add('playerCamera', { type: "entity" });


// initialize code called once per entity
PlayerController.prototype.initialize = function () {

    // Check for required components
    if (!this.entity.collision) {
        console.error("First Person Movement script needs to have a 'collision' component");
    }

    if (!this.entity.rigidbody || this.entity.rigidbody.type !== pc.BODYTYPE_DYNAMIC) {
        console.error("First Person Movement script needs to have a DYNAMIC 'rigidbody' component");
    }


};



PlayerController.prototype.moveForwardHandler = function (data) {
    data.vector.x += data.cameraDirections.forwardDirection.x;
    data.vector.z += data.cameraDirections.forwardDirection.z;
    return data;
};

PlayerController.prototype.moveBackwardHandler = function (data) {
    data.vector.x -= data.cameraDirections.forwardDirection.x;
    data.vector.z -= data.cameraDirections.forwardDirection.z;
    return data;
};


PlayerController.prototype.moveLeftHandler = function (data) {
    data.vector.x -= data.cameraDirections.rightDirection.x;
    data.vector.z -= data.cameraDirections.rightDirection.z;
    return data;
};

PlayerController.prototype.moveRightHandler = function (data) {
    data.vector.x += data.cameraDirections.rightDirection.x;
    data.vector.z += data.cameraDirections.rightDirection.z;
    return data;
};


var PlayerControlsMaps = [
    {
        keys: [pc.KEY_W, pc.KEY_UP],
        handler: "moveForwardHandler",
    },
    {
        keys: [pc.KEY_S, pc.KEY_DOWN],
        handler: "moveBackwardHandler",
    },
    {
        keys: [pc.KEY_A, pc.KEY_LEFT],
        handler: "moveLeftHandler",
    },
    {
        keys: [pc.KEY_D, pc.KEY_RIGHT],
        handler: "moveRightHandler",
    }

];




// update code called every frame
PlayerController.prototype.update = function (dt) {

    // Get camera directions to determine movement directions
    var forward = this.playerCamera.forward;
    var right = this.playerCamera.right;

    var movementVec3 = new pc.Vec3(0, 0, 0);
    for (const playerControlMap of PlayerControlsMaps) {
        for (const playerControlKey of playerControlMap.keys) {
            if (this.app.keyboard.isPressed(playerControlKey)) {
                proccesMovement = this[playerControlMap.handler]({
                    vector: movementVec3,
                    cameraDirections: {
                        forwardDirection: forward,
                        rightDirection: right
                    }
                });
                movementVec3 = proccesMovement.vector;
            }
        }
    }


    if (movementVec3.x !== 0 || movementVec3.z !== 0) {
        const movement = movementVec3.normalize().scale(dt * this.power);
        this.entity.rigidbody.applyForce(movement);

        // rotate the model
        const newAngle = 90 - (Math.atan2(movementVec3.z, movementVec3.x) * pc.math.RAD_TO_DEG);
        this.entity.setEulerAngles(0, newAngle, 0);
    }

};


@Invictus_Pandemic

var FpsController = pc.createScript(‘fpsController’);

FpsController.attributes.add(‘camera’, {

** type: ‘entity’,**

** description: ‘Optional, assign a camera entity, otherise one is created’**

});

FpsController.attributes.add(‘power’, {

** type: ‘number’,**

** default: 3500,**

** description: ‘Adjusts the speed of player movement’**

});

FpsController.attributes.add(‘lookSpeed’, {

** type: ‘number’,**

** default: 0.25,**

** description: ‘Adjusts the sensitivity of looking’**

});

FpsController.attributes.add(‘shiftFactor’, {

** type: ‘number’,**

** default: 3,**

** description: ‘Speed factor when shift is pressed’**

});

// initialize code called once per entity

FpsController.prototype.initialize = function() {

** this.force = new pc.Vec3();**

** this.eulers = new pc.Vec3();**


** var app = this.app;**


** // listen for mouse move events**

** app.mouse.on(“mousemove”, this._onMouseMove, this);**

** app.mouse.enablePointerLock();**

** // when the mouse is clicked hide the cursor**

** app.mouse.on(“mousedown”, function() {**

** app.mouse.enablePointerLock();**

** }, this);**


** // check for required components**

** if(!this.entity.collision) {**

** console.error(“Expected ‘collision’ component.”);**

** }**


** if(!this.entity.rigidbody || this.entity.rigidbody.type !== pc.BODYTYPE_DYNAMIC) {**

** console.error(“Expected DYNAMIC ‘rigidbody’ component”);**

** }**

** this.entity.collision.on(‘collisionstart’, this.onCollisionStart, this); //Check collision for jump**


};

// update code called every frame

FpsController.prototype.update = function(dt) {

** // if a camera isn’t assigned from the editor, create one**

** if(!this.camera) {**

** this._createCamera();**

** }**


** var force = this.force;**

** var app = this.app;**


** // get camara directions to determin movements directions**

** var forward = this.camera.forward;**

** var right = this.camera.right;**


** // movement**

** var x = 0;**

** var z = 0;**


** // use W-A-S-D keys to move player**

** // check for key presses**

** if(app.keyboard.isPressed(pc.KEY_A)) {**

** x -= right.x;**

** z -= right.z;**

** }**


** if(app.keyboard.isPressed(pc.KEY_D)) {**

** x += right.x;**

** z += right.z;**

** }**


** if(app.keyboard.isPressed(pc.KEY_W)) {**

** x += forward.x;**

** z += forward.z;**

** }**

** if(app.keyboard.isPressed(pc.KEY_S)) {**

** x -= forward.x;**

** z -= forward.z;**

** }**

** // Jump on spacebar**

** if (app.keyboard.wasPressed(pc.KEY_SPACE) && onGround === true) {**


** this.entity.rigidbody.applyImpulse(0,1000,0);**

** onGround = false;**

** }**

** // Process Action keys**

** if(app.keyboard.wasPressed(pc.KEY_E)) {**

** this.processRaycast(); **

** }**

** if(app.keyboard.isPressed(pc.KEY_F)) {**

** }**


** // use direction from keypresses to apply a force to the character**

** if(x !== 0 && z !== 0) {**

** force.set(x, 0, z).normalize().scale(this.power);**

** var finalForce = force.scale(app.keyboard.isPressed(pc.KEY_SHIFT) ? this.shiftFactor : 1);**

** this.entity.rigidbody.applyForce(finalForce);**

** }**


** // update camera angle from mouse events**

** this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);**


};

FpsController.prototype._onMouseMove = function(e) {

** // if pointer is disabled**

** // if the left mouse button is down update the camera from mouse movement**

** if(pc.Mouse.isPointerLocked()) {**

** this.eulers.x -= this.lookSpeed * e.dx;**

** this.eulers.y -= this.lookSpeed * e.dy;**

** }**


};

FpsController.prototype._createCamera = function() {

** this.camera = new pc.Entity();**

** this.camera.setName(“First Person Camera”);**

** this.camera.addComponent(“camera”);**

** this.entity.addChild(this.camera);**

** this.camera.translateLocal(0, 0.5, 0);**

};

FpsController.prototype.processRaycast = function() {

** var centerScreenX = this.app.graphicsDevice.width / 2;**

** var centerScreenY = this.app.graphicsDevice.height / 2;**

** var start = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.nearClip);**

** var end = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.farClip);**


** // raycast between the two points and return the closest hit result**

** var result = this.app.systems.rigidbody.raycastFirst(start, end);**

** if(result) {**

** console.log("You Hit: " + result.entity.name);**

** result.entity.fire(“activate”);**


** } else {**

** console.log(“Nothing Hit”);**

** }**

};

FpsController.prototype.onCollisionStart = function(result) {

** // Ground Check**

** if (result.other.tags.has(‘ground’)) {**

** onGround = true;**

** } **

};

I’m definitely doing something wrong, my code is clearly working, and when i add power like 456000, it moving,
at least must be 100k, this is joke, how fix that, or it should be?

1 Like

Thanks for helping @Encrypted101!

If you want to share some code, you can use the code highlight ability to make your code easier to read.

1 Like