• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

player only shoots to the right, camera doesn't follow player, enemy won't follow player (error)

U

Unkn0wn

Guest
Hello and thank you for reading this post!
I'm terribly sorry that these questions are probably really dumb!
I am making a 2d zombie shooting game as a project for school, but I'm not very good with GameMaker studio 2, in fact, I'm a complete noob..
I'm using Drand and Drop, but sometimes I use 'execute code'. Now I have three problems:
1. My player only shoots to the right.
2. The zombies won't follow the player/character (error message).
3. The camera won't follow the player/character.

Problem 1
I have a player object/sprite. It faces to the right, but of course when the player walks to the left it can't face to the right. I also made three other sprites/objects: The back of the player, the front of the player and the player walking to the left. Now I have that when one of the arrow buttons is clicked, the sprite changes to the sprite which faces that direction. This works fine. When you press the space bar, the player is supposed to fire a bullet to the direction it's facing, however, now matter which side it's facing, it shoots to the right.
This is the code I have for shooting the bullet:

(keypress - space)
Code:
bullet = instance_create_depth(x,y,0,obj_bullet) ;
bullet.speed = 10;
direction = obj_player_wright.direction;
image_angle = direction;
Problem 2
The code I have for the zombies is:

(create)
Code:
move_towards_point( player.x, y, speed = 5)
if( abs(angle_difference(180, direction)) < 90){
image_xscale = -1;
 }else{
image_xscale = 1;
 }
But everytime I try to run the game, I get this error message:
Code:
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object obj_zombie:

Variable obj_zombie.player(100002, -2147483648) not set before reading it.
 at gml_Object_obj_zombie_Create_0 (line 5) - move_towards_point( player.x, y, speed = 5)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_zombie_Create_0 (line 5)
I don't know why I get this and how to fix it.

Problem 3
The camera won't follow the player.
I feel like I've tried everything, which I obviously haven't because else it would've worked.
What I didn't try was code, because I really don't know how to use it and I don't just want to just copy code off of the internet. Does somebody know how to code this and explain it to me? Or does anybody know a way for the camera to follow the player in DnD which works a 100% ? I viewed other threads about this, but it doesn't seem to work for me.

Thank you for reading this, I hope you have a nice day!
 
A

Andy

Guest
You are setting the direction of the instance that created the bullet here:
Code:
direction = obj_player_wright.direction;
Try somthing like:
Code:
bullet = instance_create_depth(x,y,0,obj_bullet) ;
bullet.speed = 10;
bullet.direction = obj_player_wright.direction;
bullet.image_angle = bullet.direction;
Or:
Code:
with (instance_create_depth(x,y,0,obj_bullet))
{
   speed = 10;
   direction = obj_player_wright.direction;
   image_angle = direction;
}
The zombie is trying to move towards the instance "player", but the instance does not exist.
Maybe the player is not created in the room before the zombie, or the player is destroyed while the zombie still exists.
The variable also may not be set up correctly.
You can check if "player" holds the value of an instance that exists.
Try something like this:
Code:
if (instance_exists(player))
{
   move_towards_point(player.x, y, speed = 5)
   if(abs(angle_difference(180, direction)) < 90)
   {
       image_xscale = -1;
   }
   else
   {
       image_xscale = 1;
   }
}
I don’t have experience with drag and drop in GMS2. I have heard people say this video helped them with cameras, but I am unsure if it would help if you use drag and drop.
I don’t think I can be of much help with the camera question beyond this.
 
Last edited by a moderator:
U

Unkn0wn

Guest
You are setting the direction of the instance that created the bullet here:
Code:
direction = obj_player_wright.direction;
Try somthing like:
Code:
bullet = instance_create_depth(x,y,0,obj_bullet) ;
bullet.speed = 10;
bullet.direction = obj_player_wright.direction;
bullet.image_angle = bullet.direction;
Or:
Code:
with (instance_create_depth(x,y,0,obj_bullet))
{
   speed = 10;
   direction = obj_player_wright.direction;
   image_angle = direction;
}
The zombie is trying to move towards the instance "player", but the instance does not exist.
Maybe the player is not created in the room before the zombie, or the player is destroyed while the zombie still exists.
The variable also may not be set up correctly.
You can check if "player" holds the value of an instance that exists.
Try something like this:
Code:
if (instance_exists(player))
{
   move_towards_point(player.x, y, speed = 5)
   if(abs(angle_difference(180, direction)) < 90)
   {
       image_xscale = -1;
   }
   else
   {
       image_xscale = 1;
   }
}
This video about cameras may help you:
Thank you for replying!
Because I can only work on the game at school, I can't do it right now, but I will tomorrow. Thank you so much, I hope that it's going to work now!
 

pipebkOT

Member
Problem 2 (the crashing when you start)
The code I have for the zombies is:

(create)
Code:
move_towards_point( player.x, y, speed = 5)
if( abs(angle_difference(180, direction)) < 90){
image_xscale = -1;
}else{
image_xscale = 1;
}
the player object that you are calling doesn't exist, at least not by that name, when you use objects as variables you need to make sure of using their precise name. otherwise it will crash the game because it can't find any instance by that name.

INSTEAD OF

Code:
move_towards_point( player.x, y, speed = 5)
USE:

Code:
move_towards_point(obj_player_wright.x, y, speed = 5)
 
Last edited:
U

Unkn0wn

Guest
You are setting the direction of the instance that created the bullet here:
Code:
direction = obj_player_wright.direction;
Try somthing like:
Code:
bullet = instance_create_depth(x,y,0,obj_bullet) ;
bullet.speed = 10;
bullet.direction = obj_player_wright.direction;
bullet.image_angle = bullet.direction;
Or:
Code:
with (instance_create_depth(x,y,0,obj_bullet))
{
   speed = 10;
   direction = obj_player_wright.direction;
   image_angle = direction;
}
The zombie is trying to move towards the instance "player", but the instance does not exist.
Maybe the player is not created in the room before the zombie, or the player is destroyed while the zombie still exists.
The variable also may not be set up correctly.
You can check if "player" holds the value of an instance that exists.
Try something like this:
Code:
if (instance_exists(player))
{
   move_towards_point(player.x, y, speed = 5)
   if(abs(angle_difference(180, direction)) < 90)
   {
       image_xscale = -1;
   }
   else
   {
       image_xscale = 1;
   }
}
I don’t have experience with drag and drop in GMS2. I have heard people say this video helped them with cameras, but I am unsure if it would help if you use drag and drop.
I don’t think I can be of much help with the camera question beyond this.
It didn't work, I'm not sure why :( I'm still really thankful that you helped me!
 
U

Unkn0wn

Guest
the player object that you are calling doesn't exist, at least not by that name, when you use objects as variables you need to make sure of using their precise name. otherwise it will crash the game because it can't find any instance by that name.

INSTEAD OF

Code:
move_towards_point( player.x, y, speed = 5)
USE:

Code:
move_towards_point(obj_player_wright.x, y, speed = 5)
Thank you so much! I don't get an error now, but the zombie still doesn't move towards the player.
 

pipebkOT

Member
@Unkn0wn is because you need to put that code in the STEP EVENT so is excecuted every frame, NOT in the create event :)




create event only execute a code once, the step event executes the code every frame
 
Top