Moving the gun isn’t very hard, first we need to create an offset variable in the gun’s create event and assign it the number of pixels we would like it to be offset by. The solution is that now we need to move the gun ever so slightly towards the mouse to get the desired effect. This isn’t exactly finished, the gun is always behind the player no matter where the mouse is, and we want the gun to be in front of the player when he is aiming down. When we run the game, this is what we get. The easiest way to achieve this is to update the image angle with the direction. Now that we know the direction of the mouse, it is time to actually make the gun face the mouse. Var mouse_direction = point_direction(x, y, mouse_x, mouse_y) To do that we can use the point_direction function to find the direction between the gun’s current position and the current position of the mouse. After that we will need to determine what direction the gun should face. The gun will need to update it’s location to be the same as the player’s location, which we know thanks to the player passing us it’s ID. The gun’s code is going to be in the step event of the gun object. This bit of code will make objects further down the screen drawn on top of objects higher in the room Player Step Event The depth variable is used to tell Gamemaker the order that objects should be drawn in the room. This should seem very familiar if you have been using Gamemaker for a while. The player’s step is only going to be updating the player’s depth based on their current y position inside of the room. Gun = instance_create_layer(x, y, "Instances", obj_gun) Inside of the Create event we are going to be spawning a gun on the player’s x, y location, and we are going to pass through the player’s instance id over to the gun. We only need to add 2 lines of code to the create event and a single line of code inside the step event. ![]() The amount of code that we need to put inside of the player object to make this work is very minimal. We can begin by writing all of the code for the player. Once your objects have been created and your editor looks like mine, we are ready to start writing some code. The sprite for the player should be center aligned, the gun’s sprite should be middle left aligned and the wall doesn’t really matter. ![]() The player and wall sprites can be 32 * 32 and the gun can be 32 * 8. The player, wall and gun need sprites to be able to see them in the room. We can start this project by opening up GameMaker Studio 2 and creating our 4 game objects, I named mine obj_player, obj_gun, obj_wall and obj_camera_controller. I will add some wall objects just to show the camera system working, because it will be hard to see the camera’s movement on a pure black background. Building these features in GameMaker Studio 2 isn’t really all of that hard, it only requires 3 game objects to be created, a player object, a gun object and a camera controller object.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |