Jump to content


Photo

Wall walking bug *Solved*


  • Please log in to reply
3 replies to this topic

#1 Snail_Man

Snail_Man

    Level Builder

  • GMC Member
  • 859 posts
  • Version:GM8.1

Posted 08 March 2012 - 01:48 PM

I'm making a platform game, and I wanted to add some enemies that walk on floors, walls, and ceilings. If you've ever played An Untitled Story, the ice giants are a good comparison.
Here's a small diagram

O = Enemy
--> = Enemy's path
[] = block

This is how enemies walk around the block
---->O->
^ [][][][] |
| [][][][] | You get the idea.
| [][][][] |
| [][][][] |
| [][][][] V
| <------

Anyway, so I typed up some code:

{
//Make it so the enemy walks forward
if dire==0 x += 2 ;
if dire==1 y += 2 ;
if dire==2 x -= 2 ;
if dire==3 y -= 2 ;

//Check if there is an empty space in front of it
//and change the direction accordingly vvv
if dire==0 && place_free(x,y+1) { dire = 1 x -= 1 y += 1 } ;
if dire==1 && place_free(x-1,y) { dire = 2 y -= 2 x -= 1 } ;
if dire==2 && place_free(x,y-1) { dire = 3 x += 1 y -= 1 } ;
if dire==3 && place_free(x+1,y) { dire = 0 y += 1 x += 1 } ;
//Align the position around the edge of the platform ^^^


//Check if there is a wall in front of it
//and change the direction accordingly
if dire==0 && !place_free(x+2,y) {dire = 3} ;
if dire==1 && !place_free(x,y+2) {dire = 0} ;
if dire==2 && !place_free(x-2,y) {dire = 1} ;
if dire==3 && !place_free(x,y-2) {dire = 2} ;

//set the image_angle according to the direction
if dire==0 image_angle=0 ;
if dire==1 image_angle=-90 ;
if dire==2 image_angle=-180 ;
if dire==3 image_angle=-270 ;
}


However, this code makes it so the enemies fly away at the corner. I have tried changing how far the enemies align at the corners, the enemy speed, the distance for checking the floors and walls. Everything either doesn't help, or makes the problem different. By the way, this code is in the step event, and the only other event is the create which sets dire to 0.

Edited by snail man, 08 March 2012 - 09:16 PM.

  • 0

#2 Shafroni

Shafroni

    GMC Member

  • New Member
  • 38 posts
  • Version:GM8

Posted 08 March 2012 - 05:38 PM

Maybe you can just make an invisible object, so that when the enemy touches it it changes its direction. Or maybe just set a path? Circling around the objects. I hope that I helped :o.
  • 0

#3 Gamer3D

Gamer3D

    Human* me = this;

  • GMC Member
  • 1589 posts
  • Version:GM8.1

Posted 08 March 2012 - 07:36 PM

First, let's clean up the code a bit by using GM's built-in systems.

var temp_swap;
if (!place_free(x + hspeed, y + vspeed))
  direction += 90;
else if (place_free(x - vspeed, y + hspeed))
  direction -= 90;
image_angle = direction;

Oh yeah, one thing to be aware of is that image_angle affects collision detection. Turning your sprite might be moving the collision mask away. Overcome this by removing the parts that change image_angle and instead use the draw event to draw the sprite with correct rotation.
  • 0

#4 Snail_Man

Snail_Man

    Level Builder

  • GMC Member
  • 859 posts
  • Version:GM8.1

Posted 08 March 2012 - 09:01 PM

I'll try that, thanks! I already assigned a 32x32 square mask with the origin in the center to stop the image_angle messing with things.

EDIT: Wow! That works great! I am humbled by your superior coding ability.

Edited by snail man, 08 March 2012 - 09:05 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users