[SOLVED] Using applyForce and applyTorque to move entity

I have some script that makes an entity in my game move randomly, it uses applyTorque to rotate and applyForce to move in a direction, but right now it only rotates randomly, and walks along the z axis. So it doesent walk forward, it just slides on the z axis, while randomly rotating…
Its supposed to walk forward, and then when it rotates, the direction its walking should rotate too, so it always walks forward.
((The blue thing around my npc is for later use))
Here is the spaghetti:

var SentientMovementAi = pc.createScript('sentientMovementAi');

    
// Add attribute for assigning the head
SentientMovementAi.attributes.add('npcbody', {
    type: 'entity',
    description: 'Assign the npc body'
});

SentientMovementAi.attributes.add('playerentity', {
    type: 'entity',
    description: 'Assign the player, so interactions work'
});

SentientMovementAi.attributes.add('npchead', {
    type: 'entity',
    description: 'Assign the player, so interactions work'
});

SentientMovementAi.attributes.add('npcparent', {
    type: 'entity',
    description: 'Assign the parent'
});

SentientMovementAi.attributes.add('npcspeed', {
    type: 'number',
    default: 2500,
    description: 'Assign the npc walking speed'
});

    let restTime = Math.floor(Math.random() * 10000) + 2000;

    let npcRotation = 0;

    let rotating = false;

    let walking = false;

    let standTime = Math.floor(Math.random() * 15000) + 5000;

    let walkspeed = Math.floor(Math.random() * 10);

    function sentientRotation() {
        rotating = true;
        restTime = Math.floor(Math.random() * (15000 - 3000 + 1) + 3000);
        setTimeout(() => {
            npcRotation = Math.floor(Math.random() * (9 - 6 + 1) + 6);
            console.log("Starting_ROTATE_" + restTime + "_" + npcRotation);

            rotating = false;
        }, restTime);
    }

    // Sentient walking
    function sentientWalking() {
        walking = true;
        standTime = Math.floor(Math.random() * (15000 - 3000 + 1) + 3000);
        setTimeout(() => {
            walkspeed = Math.floor(Math.random() * (6000 - 4000 + 1) + 4000);
            console.log("Starting_WALK_" + standTime + "_" + walkspeed);

            walking = false;
        }, standTime);
    }

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

};

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

    //sentient rotation
    if(rotating == false) {
        sentientRotation();
        setTimeout(() => {
            this.npcparent.rigidbody.angularVelocity = pc.Vec3.ZERO;
            npcRotation = 0;
            console.log("Stopping_ROTATE");
        }, 3000);
    }
    this.npcparent.rigidbody.applyTorque(0, npcRotation, 0);

    //sentient walking
    if (walking == false) {
        sentientWalking();
            setTimeout(() => {
            this.npcparent.rigidbody.linearVelocity = pc.Vec3.ZERO;
            walkspeed = 0;
            console.log("Stopping_WALK");
        }, 3000);
    }

    this.npcparent.rigidbody.applyForce(0, 0, -walkspeed);

    // this.npcbody.lookAt(this.playerentity.getPosition());
};

1 Like

Hi @ThyDev ,

The reason that the character is only moving along the Z axis is because the applyForce function operates specifically in world space. If you would like the character to move in the direction that it is facing you will want to use the character’s forward vector to apply a force along in order to move it. A good, simple example of how that might be achieved is in the API reference here:

https://developer.playcanvas.com/api/pc.RigidBodyComponent.html#applyForce

You’ll see that the 3rd and 4th code blocks in that section deal directly with getting the appropriate vector from the entity’s forward axis before applying the force.

I hope this is helpful.

3 Likes

Thanks! it works as intended now :slight_smile: