I'm createing a game for a school project, and I need to check when a player collides with a wall, but the function I've written only works for one side of the rectangle
function wallCollision(){
if (player.x + player.l > wall.x && player.x < wall.x + wall.width){
if (player.y == wall.y + wall.height){
player.velocity.negy = 0;
}
if (player.y + player.l == wall.y){
player.velocity.posy = 0;
}
}
if (player.y < wall.y + wall.height && player.y + player.l > wall.y){
if (player.x == wall.x + wall.width){
player.velocity.negx = 0;
}
if (player.x + player.l == wall.x){
player.velocity.posy = 0;
}
}
}
When I run it only works when checking id the player collided on the left side of the wall
if (player.x == wall.x + wall.width){
player.velocity.negx = 0;
}
and when I tried to console.log inside the other 3 nested ifs I discovered that the code insade never runs
I've tried riarranging the function but it either didn't work completely or was somehow worse
neg and pos refer to the direction on the x and y axis, so posx = 0 means that the player cannot move right, negx = 0 that it cannot move let, posy = 0 that it cannot go downwards and negy = 0 that it cannot move upwards