[SOLVED] setPosition not working for rigid body

Here is my script:

var FirstPersonMovement = pc.createScript('firstPersonMovement');

FirstPersonMovement.attributes.add('camera', {
    type: 'entity',
    description: 'Optional, assign a camera entity, otherwise one is created'

FirstPersonMovement.attributes.add('power', {
    type: 'number',
    default: 2500,
    description: 'Adjusts the speed of player movement'

FirstPersonMovement.attributes.add('lookSpeed', {
    type: 'number',
    default: 0.25,
    description: 'Adjusts the sensitivity of looking'

FirstPersonMovement.attributes.add('gunpower', {type: 'number', default:1});

// initialize code called once per entity
FirstPersonMovement.prototype.initialize = function() {
    // this.entities = []; //creating an array to affect later
    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);

    // when the mouse is clicked hide the cursor
    app.mouse.on("mousedown", function () {
    }, this);

    // 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");

// update code called every frame
FirstPersonMovement.prototype.update = function(dt) {
    // If a camera isn't assigned from the Editor, create one
    if (!this.camera) {

    var force = this.force;
    var app = this.app;

    // Get camera directions to determine movement 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) || app.keyboard.isPressed(pc.KEY_Q)) {
        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;

    // 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);

    // update camera angle from mouse events
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);

FirstPersonMovement.prototype.shoot = function(dt){
    // clone bullet
    var bullet = this.testObject.clone();
    // add to the game
    var player = this.entity;
    var gun = this.app.root.findByName('gun');
    this.force = new pc.Vec3();
    var pos = gun.getPosition();
    var direction = gun.forward;
    bullet.setPosition( pos );
    bullet.enabled = true;

FirstPersonMovement.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() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;

FirstPersonMovement.prototype._createCamera = function () {
    // If user hasn't assigned a camera, create a new one
    this.camera = new pc.Entity();
    this.camera.setName("First Person Camera");
    this.camera.translateLocal(0, 0.5, 0);

Currently, the bullets are flying upwards out of my gun, does anybody know how to fix it to make the bullets fly forwards?

The problem here is that you are using setPosition to set the initial position of the bullet entity. The bullet entity is a rigid body and setPosition, setRotation and so on should not be used with rigid bodies, because the physics engine is responsible for updating their transforms. This is explained here:


So you should change:

    bullet.setPosition( pos );


    bullet.rigidbody.teleport( pos );

I do recommend reading that whole User Manual section on Physics.

thanks for helping

1 Like

Iā€™m not sure If i have done something wrong, but I replaced the script for what you recommended, and the bullets still fly upwards.

I recommend you look at the project I quickly set up using your script:


It seems to work OK.

1 Like

Do you know if the gun forward direction pointing in the direction of barrel?

If you can share the model or the project, that would really help?

It works now, thank you so much for your help.

1 Like