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