Dashing Mechanic

So, for my game I have forked a simple movement script and edited it to include an air dash mechanic. The mechanic is supposed to allow the player one air dash (direction of their choice) per jump.

I established a variable “airborne” to make it so you can’t dash while on the ground. However, when I try to restrict the number of dashes (by setting it to false after the conditions are met to apply the impulse) it doesn’t work.

I’ve tired reworking the script and reordering the commands after the condition is met, however, it still doesn’t work. Could someone provide assistance as to what’s going on?

Script Below


s
var Heyehyey = pc.createScript('heyehyey');
​
// initialize code called once per entity
Heyehyey.prototype.initialize = function() {
    
};
​
// update code called every frame
Heyehyey.prototype.update = function(dt) {
       if(this.app.keyboard.isPressed(pc.KEY_D)){
    this.entity.rigidbody.applyForce(0,0,5000);}
    
       if(this.app.keyboard.isPressed(pc.KEY_A)){
    this.entity.rigidbody.applyForce(0,0,-2000);} 
    
    /*
       if(this.app.keyboard.isPressed(pc.KEY_SHIFT)){
    this.entity.rigidbody.applyForce(-1800,0,0);} 
    
       if(this.app.keyboard.isPressed(pc.KEY_SHIFT)){
    this.entity.rigidbody.applyForce(1800,0,0);} 
    */
    
    //Jumping
    var canJump = false;
    var from = this.entity.getPosition();
    var to = new pc.Vec3(from.x, from.y - this.entity.getLocalScale().y, from.z);
    var result = this.app.systems.rigidbody.raycastFirst(from, to);
    var toLeftSide = new pc.Vec3(from.x - this.entity.getLocalScale().x, from.y, from.z);
    var resultLeftSide = this.app.systems.rigidbody.raycastFirst(from, toLeftSide);
    var toRightSide = new pc.Vec3(from.x + this.entity.getLocalScale().x, from.y, from.z);
    var resultRightSide = this.app.systems.rigidbody.raycastFirst(from, toRightSide);
    var airborne = false;
    var dash = this.app.keyboard.wasPressed(pc.KEY_SPACE);
    var jump = this.app.keyboard.wasPressed(pc.KEY_W);
    
    if(result || resultLeftSide || resultRightSide)
    {
        canJump = true;
        airborne = false;
    }
    else
    {
        canJump = false;
        airborne = true;
    }
    
    
    if(this.app.keyboard.wasPressed(pc.KEY_W) && canJump)
    {
        if(resultLeftSide && !result)
            this.entity.rigidbody.applyImpulse(0,1200,1000);
        else if(resultRightSide && !result)
            this.entity.rigidbody.applyImpulse(0,1200,-1000);
        else if(result)
            this.entity.rigidbody.applyImpulse(0,1200,0);
    }
    
    if(airborne &dash == true)
{
        if(dash && !jump)
            airborne = false;
            this.entity.rigidbody.applyImpulse(0,2000, 0);
    
    
            
}
};
​
masterSaved "heyehyey.js"Connected

Hi @Overbaked_Prod!

I think this statement is not correct.

Hello, Albertos.

I’ve checked over the code and changed it to:

if(airborne && dash == true)

However, even when doing so, the problem pursist.

Alright.

Can you explain a little better what exactly is not working? I don’t know what you try to achieve because the Dutch translation of the word ‘dash’ is not very clear. :sweat_smile:

I think the jump statement here is almost always false because it’s only one frame true after the player pressed the W key. Is that what you expect?

Yeah, I noticed that so I tried implementing a onGround function. However, the code is still proven to not work. Now I think it’s applying the “dash” impulse instead of allowing the character to jump.

Code Below:

var Heyehyey = pc.createScript('heyehyey');

// initialize code called once per entity
Heyehyey.prototype.initialize = function() {
    
};

// update code called every frame
Heyehyey.prototype.update = function(dt) {
       if(this.app.keyboard.isPressed(pc.KEY_D)){
    this.entity.rigidbody.applyForce(0,0,5000);}
    
       if(this.app.keyboard.isPressed(pc.KEY_A)){
    this.entity.rigidbody.applyForce(0,0,-2000);} 
    
    /*
       if(this.app.keyboard.isPressed(pc.KEY_SHIFT)){
    this.entity.rigidbody.applyForce(-1800,0,0);} 
    
       if(this.app.keyboard.isPressed(pc.KEY_SHIFT)){
    this.entity.rigidbody.applyForce(1800,0,0);} 
    */
    
    //Jumping
    var canJump = false;
    var from = this.entity.getPosition();
    var to = new pc.Vec3(from.x, from.y - this.entity.getLocalScale().y, from.z);
    var result = this.app.systems.rigidbody.raycastFirst(from, to);
    var toLeftSide = new pc.Vec3(from.x - this.entity.getLocalScale().x, from.y, from.z);
    var resultLeftSide = this.app.systems.rigidbody.raycastFirst(from, toLeftSide);
    var toRightSide = new pc.Vec3(from.x + this.entity.getLocalScale().x, from.y, from.z);
    var resultRightSide = this.app.systems.rigidbody.raycastFirst(from, toRightSide);
    var airborne = false;
    var dash = this.app.keyboard.wasPressed(pc.KEY_SPACE);
    var jump = this.app.keyboard.wasPressed(pc.KEY_W);
    var onGround = false;
    
    Heyehyey.prototype.onCollisionStart = function(result){
    //if(result.other.name == 'ground'){
    if(result.other.tags.has('ground')){
      onGround = true;  
    }
};
    
    Heyehyey.prototype.onCollisionEnd = function(other){
    if(other.tags.has('ground')){
      onGround = false;  
    }

};
    if(result || resultLeftSide || resultRightSide)
        if(onGround === true)
    {
        canJump = true;
        airborne = false;
    }
    else
    {
        canJump = false;
        airborne = true;
    }
    
    
    if(this.app.keyboard.wasPressed(pc.KEY_W) && canJump)
        if(onGround === true)
    {
        if(resultLeftSide && !result)
            this.entity.rigidbody.applyImpulse(0,1200,1000);
        else if(resultRightSide && !result)
            this.entity.rigidbody.applyImpulse(0,1200,-1000);
        else if(result)
            this.entity.rigidbody.applyImpulse(0,1200,0);
    }
    
    if(airborne && dash == true)
{
        if(dash && !jump)
            airborne = false;
            this.entity.rigidbody.applyImpulse(0,2000, 0);
    
    
            
}
};

Can you tell me what is ‘dashing’ please?

As far as I can see you don’t use the new onGround variable for your ‘dash’ action?