1 decade ago by TommyBs
Hi,
Does anyone have an idea of how I would handle friction in 1 direction only (y-axis). I've got a top down "driving" game (it's not a car but requires up,down,left,right movement). If the player is moving up or down, I want them to accelerate and slowly come to a halt if they remove their hand from the direction keys. However I want the player to be able to move left and right, but instantly stop on the x-axis. The below sort of works, though the player won't move left and only seems to move right when going up or down:
//props:
maxVel:{x:200,y:200},
accelGround:50,
brakeSpeed:20,
friction:{x:10000, y:50},
// code in update() to handle movement
if(ig.input.state('left')){
this.accel.x = -this.accelGround;
}else{
this.accel.x = 0;
}
if(ig.input.state('right')){
this.accel.x = this.accelGround;
}else{
this.accel.x = 0;
}
if(ig.input.state('up')){
this.accel.y = -this.accelGround;
}else if(ig.input.state('down')){
this.accel.y = this.accelGround;
}else{
this.accel.x = 0;
this.accel.y = 0;
this.currentAnim = this.anims.idle;
}
I understand why the if-else above isn't really working, as the else conditions further down are firing and setting the accel to 0. But when I try it as follows, the friction on x doesn't seem to apply:
if(ig.input.state('left')){
this.accel.x = -this.accelGround;
}else if(ig.input.state('right')){
this.accel.x = this.accelGround;
}else if(ig.input.state('up')){
this.accel.y = -this.accelGround;
}else if(ig.input.state('down')){
this.accel.y = this.accelGround;
}else{
this.accel.x = 0;
this.accel.y = 0;
this.currentAnim = this.anims.idle;
}
It's late here as I type this, but a thought just occured to me. Am I better of setting this.vel.x = 0; Rather than the accel for left and right?
Thanks
Does anyone have an idea of how I would handle friction in 1 direction only (y-axis). I've got a top down "driving" game (it's not a car but requires up,down,left,right movement). If the player is moving up or down, I want them to accelerate and slowly come to a halt if they remove their hand from the direction keys. However I want the player to be able to move left and right, but instantly stop on the x-axis. The below sort of works, though the player won't move left and only seems to move right when going up or down:
//props:
maxVel:{x:200,y:200},
accelGround:50,
brakeSpeed:20,
friction:{x:10000, y:50},
// code in update() to handle movement
if(ig.input.state('left')){
this.accel.x = -this.accelGround;
}else{
this.accel.x = 0;
}
if(ig.input.state('right')){
this.accel.x = this.accelGround;
}else{
this.accel.x = 0;
}
if(ig.input.state('up')){
this.accel.y = -this.accelGround;
}else if(ig.input.state('down')){
this.accel.y = this.accelGround;
}else{
this.accel.x = 0;
this.accel.y = 0;
this.currentAnim = this.anims.idle;
}
I understand why the if-else above isn't really working, as the else conditions further down are firing and setting the accel to 0. But when I try it as follows, the friction on x doesn't seem to apply:
if(ig.input.state('left')){
this.accel.x = -this.accelGround;
}else if(ig.input.state('right')){
this.accel.x = this.accelGround;
}else if(ig.input.state('up')){
this.accel.y = -this.accelGround;
}else if(ig.input.state('down')){
this.accel.y = this.accelGround;
}else{
this.accel.x = 0;
this.accel.y = 0;
this.currentAnim = this.anims.idle;
}
It's late here as I type this, but a thought just occured to me. Am I better of setting this.vel.x = 0; Rather than the accel for left and right?
Thanks