Creating objects programatically and adding constant force

Hi all,

New to Playcanvas and trying some things out. I’m attempting to spawn cubes at a random position every second and give it a constant, linear force of 4.0f on the X-axis. Everything works up to the force part, where my cubes just refuse to move. My code is as follows:

EnemySpawner.prototype.spawnEnemy = function() {
    var entity = new pc.Entity();
    entity.addComponent("model", {
        type: 'box', 
        castShadows: false
    });
    
    entity.addComponent("rigidbody", {
        type: 'kinematic',
    });
    
    entity.setPosition(
        pc.math.random(-10, 10),
        pc.math.random(-10, 10),
        pc.math.random(-10, 10)
    );
    
    entity.rigidbody.linearVelocity = new pc.Vec3(4, 0, 0);
    this.app.root.addChild(entity);
};

As I said, everything there works except for entity.rigidbody.linearVelocity = new pc.Vec3(4, 0, 0);. My objects do not move at all.
I believe the issue is somehow related to the creation of the rigid body component? Not sure.

Any help is appreciated!

Rigid bodies need a collision component as well. Also don’t forget to enable physics in the project settings.

1 Like