M
MagusMori
Guest
Hey everyone, I'm pretty new to using Gamemaker and coding in general. More of an artist but I'm trying to learn how to make games and put my skills to use!
Anyway, I've been looking through a lot of posts and tutorial videos and managed to get myself a working player character in a pixel platformer style setting. But now I want to add an AI animal that wanders around but doesn't attack the player. I got the wandering working really well, but it isn't animated and doesn't collide when it is supposed to. All the examples I've found of tutorials don't seem to go into the animation of non-player walking objects. From what I have learned so far I tried to write some code for it based off my player characters animations and collisions, but it isn't working. Any help to understand the issue and get this working would be much appreciated!
tldr;
I'm working on adding in a 'friendly' animal AI that wanders around. The wandering part is working great, but I can't figure out how to add collisions and animation when he moves for some reason. Can anyone help me understand why it doesn't use the codes I wrote and how I could better write it to get it working?
Here is the 'create' event:
Here is the 'step' event:
Anyway, I've been looking through a lot of posts and tutorial videos and managed to get myself a working player character in a pixel platformer style setting. But now I want to add an AI animal that wanders around but doesn't attack the player. I got the wandering working really well, but it isn't animated and doesn't collide when it is supposed to. All the examples I've found of tutorials don't seem to go into the animation of non-player walking objects. From what I have learned so far I tried to write some code for it based off my player characters animations and collisions, but it isn't working. Any help to understand the issue and get this working would be much appreciated!
tldr;
I'm working on adding in a 'friendly' animal AI that wanders around. The wandering part is working great, but I can't figure out how to add collisions and animation when he moves for some reason. Can anyone help me understand why it doesn't use the codes I wrote and how I could better write it to get it working?
Here is the 'create' event:
Code:
/// Initialize Gooy AI
image_speed = 0.08
hsp = 0;
vsp = 0;
grav = 0.2;
movespeed = 1;
// wander ai
direction_choose=choose(0,180); //random direction
rand_timer=random_range(room_speed,room_speed * 2); //random interval
rand_dis=random_range(32,64); //random distance
move_speed=0.5;
destX=x;
destY=y;
Code:
/// Control Gooy AI
// took this from my player walk script to try to apply it here, doesn't appear to work
move = (hsp!=0)
hsp = move * movespeed;
if (vsp < 10) vsp += grav;
// wander ai (this works!)
if (rand_timer>0)
{
//move to the place, tick down the timer
rand_timer--;
direction=direction_choose;
if (distance_to_point(destX,destY)<=0) {speed=0;} else {speed=move_speed;}
}
else
{
//direction and distance
direction_choose=choose(0,180);
rand_dis=random_range(32,64);
//destination
//choose spot that it isn't off limits (how???)
destX= x+lengthdir_x(rand_dis,direction_choose);
destY= y+lengthdir_y(rand_dis,direction_choose);
//reset the timer
rand_timer=random_range(room_speed * 8 ,room_speed * 15);
}
// gooy flip, collision and animation
//face the sprite's current walking direction (this works!)
if(direction>89 && direction<271)
then
{
image_xscale=-1;
}
else
{
image_xscale=1;
}
// horizonal collision (not working, taken from player walk script)
if (place_meeting(x + hsp, y, obj_wall))
{
while (!place_meeting(x+sign(hsp),y,obj_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
// animate (not working, taken from player walk script)
if (place_meeting(x,y+1,obj_wall)) //on the floor?
{
if (move!=0) sprite_index = spr_gooy_run;
else sprite_index = spr_gooy_idle;
}