Car system, local coordinates, and applyforce

I am making a system for a car, and I am trying to make it follow local coordinates to move forward. I have looked up everything, and everything I tried hasn’t worked. Currently, it is using applyForce to move, and it only moves along one axis. For clarity, I am making a car which turns, not a car which only moves.
My project: https://playcanvas.com/editor/scene/2420385

Hi @Aksel_Walztoni! :wave:

The force is applied in world space, so even if your car rotates, a force will always push along the world axis. That’s why it feels like it only moves on one axis.

To move forward relative to the car’s rotation, you need to apply force in the entity’s forward direction instead of a fixed axis.

var Movement = pc.createScript('movement');

Movement.prototype.initialize = function () {
    this.force = 100000;
    this.torque = 4000;
};

Movement.prototype.update = function (dt) {
    if (this.app.keyboard.isPressed(pc.KEY_W)) {
        var forward = this.entity.forward.clone().scale(-this.force);
        this.entity.rigidbody.applyForce(forward);
    }

    if (this.app.keyboard.isPressed(pc.KEY_S)) {
        var backward = this.entity.forward.clone().scale(this.force);
        this.entity.rigidbody.applyForce(backward);
    }

    if (this.app.keyboard.isPressed(pc.KEY_A)) {
        this.entity.rigidbody.applyTorque(0, this.torque, 0);
    }

    if (this.app.keyboard.isPressed(pc.KEY_D)) {
        this.entity.rigidbody.applyTorque(0, -this.torque, 0);
    }
};

In the forked project below, I reset the parent rotation to 0 and rotated the model to -90 instead. I repositioned the camera and adjusted the rigidbody settings. I also removed a duplicate scripted Box entity.

https://playcanvas.com/editor/scene/2421038

1 Like