How to apply gravity on specific entity, when in air?

I have play when he jump, or jump from obstacle, he seems like it just flying, in physics setting, u can apply for all bodies, but i need for specific and specific, i asked chatgpt it give me this

// get the player's rigidbody component
var rigidbody = playerEntity.rigidbody;

// calculate the direction of gravity
var gravity = new pc.Vec3();
app.systems.rigidbody.gravity.grab(gravity).normalize();

// check if the player is in the air (not grounded)
if (!grounded) {
    // apply a force in the opposite direction of gravity to make the player fall
    rigidbody.applyForce(gravity.scale(-1.0).scale(fallForce));
}

But when i add, i have problem with getting gravity, so i tried, to apply force, for the y in -4, but nothing and after few second, screen becomes just black.

So how apply gravity for my player when not grounded?
Tried this

var gravity = this.reuseVec3.set(-3, -3, -3);
this.entity.rigidbody.applyForce(gravity.scale(this.airGravity));

But after this, i got bugs if set like 10k(cause 1k is almost nothing), i got like bounce teleporting, and ofc, go outside any collision and rigid bodies

Hi @Invictus_Pandemic! What about just applying a downwards force on the rigidbody of the entity, when the entity is in the air?

1 Like

@Invictus_Pandemic Here is some code where I do this.

// Raycast to see if we are grounded or on solid
FpsController.prototype.isGrounded = function(dt) {
    this.grounded = false;

    // Setup raycast down
    var raycastStart = this.entity.getPosition();
    var raycastEnd = new pc.Vec3();
    raycastEnd.add2(raycastStart, new pc.Vec3(0,-1.25,0));

    var result = this.app.systems.rigidbody.raycastFirst(raycastStart, raycastEnd);

    if(result != null) {

        if(result.entity.tags.has('road','ground')) this.grounded = true;
    }

    // Handle grounded behavior
    if(this.grounded) {

        //console.log("Ground");

    } else {

        this.entity.rigidbody.applyImpulse(0,-75,0);  

    }    

};
1 Like