N
Nirwins
Guest
Hi all.
I'm having a strange issue with collision for my enemy object while in its "attack" state in my 2D platformer. While in this state the enemy moves towards the player object if there is no collision with the wall in front of it. This works perfectly with any wall to the right of the enemy, and it stops at the wall, but when moving to the left, the enemy runs straight through any wall.
I've watched multiple tutorials trying to figure out my issue, but I cannot figure out how to fix this.
Apologies if my code isn't very good, I only started learning recently.
Here is the code for the step event in my enemy object:
I'm having a strange issue with collision for my enemy object while in its "attack" state in my 2D platformer. While in this state the enemy moves towards the player object if there is no collision with the wall in front of it. This works perfectly with any wall to the right of the enemy, and it stops at the wall, but when moving to the left, the enemy runs straight through any wall.
I've watched multiple tutorials trying to figure out my issue, but I cannot figure out how to fix this.
Apologies if my code isn't very good, I only started learning recently.
Here is the code for the step event in my enemy object:
GML:
vsp = vsp + grv;
//Horizontal collision
if (place_meeting(x+hsp,y,oWall))
{
while (!place_meeting(x+sign(hsp),y,oWall))
{
x = x + sign(hsp);
}
hsp = -hsp;
}
//Vertical collision
if (place_meeting(x,y+vsp,oWall))
{
while (!place_meeting(x,y+sign(vsp),oWall))
{
y = y +sign(vsp);
}
vsp = 0;
}
y = y + vsp;
//Jumping
if place_meeting(x+5*sign(hsp),y,oTrigger)
{
vsp = -5;
}
if(state == states.idle){
#region Idle
//Behavior
counter += 1;
//Transition triggers
if(counter >= room_speed){
var change = choose(0,1);
switch(change){
case 0: state = states.wander;
case 1: counter = 0; break;
}
}
if(collision_circle(x,y, 250, oPlayer, false, false)){
state = states.alert;
}
//Sprite
sprite_index = sEnemy;
#endregion
}
else if(state == states.wander){
#region Wander
//Behavior
counter += 1;
x = x + hsp;
//Transition triggers
if (counter >= room_speed){
var change = choose(0,1,2);
switch(change){
case 0: state = states.idle;
case 1: x = x + hsp;
case 2: x = x - hsp;
}
}
if(collision_circle(x,y, 200, oPlayer, false, false)){
state = states.alert;
}
//Sprite
sprite_index = sEnemyR;
image_xscale = sign(hsp);
#endregion
}
else if(state == states.alert){
#region Alert
//Behavior
//Transition triggers
if(!collision_circle(x, y, 200, oPlayer, false, false)){
state = states.idle
}
if(collision_circle(x, y, 100, oPlayer, false, false)){
state = states.attack;
}
//Sprite
sprite_index = sEnemy;
var aimside = sign(oPlayer.x - x);
if (aimside != 0) image_xscale = aimside;
#endregion
}
else if(state == states.attack)
{
#region Attack
if (instance_exists(oPlayer))
{
//Behavior
var dir = sign(oPlayer.x - x);
if (dir != 0) and (!place_meeting(x*dir,y,oWall))
{
hitwall = false;
hsp = dir * walksp;
x = x + hsp;
}
else hitwall = true;
//Transition triggers
if(!collision_circle(x, y, 200, oPlayer, false, false))
{
state = states.idle;
}
//Sprite
if (hitwall = false) sprite_index = sEnemyR;
else sprite_index = sEnemy;
image_xscale = dir;
}
}
#endregion
if (!place_meeting(x,y+1,oWall))
{
grounded = false;
sprite_index = sEnemyA;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
grounded = true;
image_speed = 1;
}
Last edited by a moderator: