Gravity for game not working

Here is the editor and this is what the game looks like from the only current planets ground:

I want to have a dynamic player impelemnted to the game, and give it the tags “111” so the script will then get the player entity then pull the player towards the planet with a feild of gravity but the current script is not functional even when I disabled gravity. The script SHOULD get the entity with the tag “111” and pull it to the planet. Here is the current script:

var GravityScript = pc.createScript('gravityScript');

GravityScript.attributes.add('pullForce', { type: 'number', default: 5 }); // Adjust the pullForce value as needed

// Called every frame
GravityScript.prototype.update = function(dt) {
    var entities = this.app.root.findByTag('111');

    for (var i = 0; i < entities.length; i++) {
        var entity = entities[i];
        var direction = this.entity.getPosition().clone().sub(entity.getPosition()).normalize();
        var force = direction.scale(this.pullForce * dt);

        entity.rigidbody.applyForce(force);
    }
};

If anyone knows what’s wrong please let me know!

Hi @Jacob_McBride2,

Your Box entity that has this script attached has a Static rigidbody. You will need to set it to dynamic to be able to apply forces/impulses and changes in its velocity.

image

2 Likes

except the entity with the script should be still, and the other entity should be pulled by the entity with the script.

Try doing a forum search, there have been some posts on the subject, for example:

I’ve seen that one but it did not help. I just need the dynamic entity pulled to the static entity.

There isn’t an API to do that, so you will need to implement it in code e.g. using some of the ideas shared in that post.

1 Like

Nah I fixed it just now. All I had to do was make the planet stop rotating so I could get a good view of what happens, then I gave the planet gravity, and now the planet has gravity.

However it does have a weird effect at the bottom of the planet, the boxes orbit under the plaet strangly, this is not what I need. i need gravity pulling from all sides to the ground.