Extreme rigidbody force glitch

I have built a game, Hydra Run, that allows users to jump, and I accomplish this by (1) Adding an initial force on the first frame of the jump, and (2) Allowing some extra jump force if the user continues pressing.

The bug I’m facing is that infrequently, unpredictably, and unjustifiably, the force will be so great that the character is shot way off screen and takes around 10 seconds just to fall back down.

Here’s the only code the manipulates the jump force:


PlayerMovement.prototype.jump = function () {
    if(self.jumpTimerRecent < 0) {
        if(!this.isJumping() && timeOnGround > 0.035) {
            this.entity.rigidbody.applyForce(0, this.initalJumpForce, 0);
            self.jumpTimerRecent = 0.03;
            self.jumpExtraTimeAllowed = this.extraJumpTime;
            self.timeOnGround = 0;
        }
    }
    
    if(self.jumpExtraTimeAllowed > 0) {
        this.entity.rigidbody.applyForce(0, this.extraJumpForce, 0);
    } else {
        // resistance force
        if( this.entity.rigidbody.linearVelocity.y < -0.5) {
            this.entity.rigidbody.applyForce(0, this.extraJumpForce*(-this.entity.rigidbody.linearVelocity.y), 0);
        }
    }

};

Any ideas as to why this is happening?

At a guess, it might be something to do with the player’s linear velocity mod. I suggest logging the forces being applied to console and seeing why it’s so high or being applied for any length of time.

On a side note, if you want to jump, applyImpulse on a single frame might give you a better effect as it changes the velocity of the rigidbody immediately.

Thanks for the feedback. I have logged it and it never applies the initial force more than once. Also, applyImpulse literally just teleports the player way off screen, so I’ve avoided it like the plague since my initial issues with it

I would also log how much force is applied in that one frame.

In which case, I would say that you applied too much impulse. Lower the values.

1 Like

Hi @haidarcorp,

What is Hydra? It might be interesting to hear a bit about the system / environment, including some stats about user base etc ( I did a quick search after seeing one of your previous projects, but didn’t find much ).

It might be worth doing a quick post about it, if it’s a potential new source of revenue for some of the games being developed in PlayCanvas.

This is something we’d be glad to do, we appreciate the suggestion.

Hydra is still in early beta, only has around 1,000 active users, and is scheduled to beta launch on iOS and Web in Q3 of 2019. However, we will definitely post when our Gaming API is fully defined and demonstrated in our demo game, so hopefully in early March.

It’s definitely a new source of revenue for every game on PlayCanvas specifically, as we’re spending a great deal of time implementing extremely developer-friendly cross-platform functionality across the entire platform :slight_smile:

1 Like