How to prevent double jump, if grounded variable have, and seted raycast?

There have specific time when i jump, in that delay i can press jump and it jump again, so it making double jump. code is there. event of keyboard for jump is “wasPressed” and i check if is grounded and ready to jump
Editor link: PlayCanvas | HTML5 Game Engine

    var playerPosition = this.entity.getPosition();
    var groundRayCastYInAir = 0.9;

    if (!this.onGround) {
        groundRayCastYInAir = 0.3;
    }


    var raycastTo = this.reuseVec3.set(playerPosition.x, playerPosition.y - groundRayCastYInAir, playerPosition.z);

    var launchGroundRaycast = this.processRayCastFirstHit(playerPosition, raycastTo);
    if (launchGroundRaycast) {
        isGrounded = this.processRayCastFirstHitResultByTag(launchGroundRaycast, 'ground');
        if (isGrounded) {
            this.processGroundedModification();
        }
    }
PlayerController.prototype.processGroundedModification = function () {

    this.onGround = true;
    this.readyToJump = true;

};

PlayerController.prototype.jumpHandler = function (data) {
    this.entity.rigidbody.linearVelocity.y = 0;
    this.entity.rigidbody.applyImpulse(0, this.jumpPower, 0);
    this.onGround = false;
    this.readyToJump = false;
    return data;

};


Anyone have ideas? i setted as low raycast as possible to just it works, but still, i could perform shty double jump, as one solution is create, delayed reseting jump, like, if u jumped, wait 1 second. Or i shoud create like empty object and set it to the bottom of entity, and raycast from there, how to get playerHeight in playcanvas, this so hard to find solutions, since it a f different from other game engines.

Like i tried this

var playerPosition = this.entity.getPosition();
var rayStart = new pc.Vec3(playerPosition.x, playerPosition.y - (entityScale.y / 2), playerPosition.z);
var rayEnd = new pc.Vec3(rayStart.x, rayStart.y - 1, rayStart.z);

But this is worst(upper code), than my previously code(below code).

var playerPosition = this.entity.getPosition();
var groundRayCastYInAir = 0.9;

if (!this.onGround) {
    groundRayCastYInAir = 0.3;
}

var raycastTo = this.reuseVec3.set(0, playerPosition.y - groundRayCastYInAir, 0);

Also i tried from collision detection, but for example if close to wall, and of course on some part flat wall i can jump, and just standing, aside, i can just make double jump.

PlayerController.prototype._onCollisionStart = function (result) {

    if (result.other.tags.has('ground') && !this.onGround) {

        this.onGround = true;
        this.readyToJump = true;

    }

};

@Invictus_Pandemic

So in the above the following occurs. I don’t know what the rest of the code does.

If you find the ‘ground’ tag and this and this.onGround = false then this condition is satisfied and because of the ! sign. So in this case you entity is in the air? readyToJump is true so you can jump again?

What I would do is check your conditions here with a console.log.

It is also a good idea to check result before you evaluate it like

   // raycast between the two points and return the closest hit result
    var result = this.app.systems.rigidbody.raycastFirst(start, end);

    if(result) {

        //Now you have a valid result so check it.
    }
1 Like

@Tirk182
In collision that code, i just test everything and that’s why i put there !this.onGround, even if remove the problem is the same. var readyToJump just by tutor, that’s why it still there, but that var do nothing, since i’m not implement this. Ray cast of course i check

var playerPosition = this.entity.getPosition();
var groundRayCastYInAir = 0.9;

if (!this.onGround) {
    groundRayCastYInAir = 0.3;
}

var raycastTo = this.reuseVec3.set(playerPosition.x, playerPosition.y - groundRayCastYInAir, playerPosition.z);

var launchGroundRaycast = this.processRayCastFirstHit(playerPosition, raycastTo);
if (launchGroundRaycast) {
    isGrounded = this.processRayCastFirstHitResultByTag(launchGroundRaycast, 'ground');
    if (isGrounded) {
        this.processGroundedModification();
    }
}

Code there PlayCanvas | HTML5 Game Engine
Editor there PlayCanvas | HTML5 Game Engine

But i think in playcanvas better is to make like cylinder and set to bottom, and check if it on ground, jump, this is coud prevent double jump, and i’m not able to stay close aside of wall make double jump, and just jump on that high wall…