• 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!

Windows Player Movement: Shooting

F

Freedom2Fight

Guest
Hello, again. I would like to request your help.

I am making a platformer. I am relatively new to this and I am very eager to learn.

So here is the situation:

I want the playable character to able to shoot projectiles in the direction they are facing.

Whenever I press the [Attack Button] the character will [shoot] and a [bullet will launch] in the [direction they are facing]. Please see illustration below.



It also doubles as a rorschach test

The code I provided below allows the player shoot. However there is a problem, please see illustration below:




There are also a number of issues I have yet to deal with such as the bullet destroying itself upon collision with walls and enemies. Right now, I just want the bullet to travel to the correct direction.

Here is the code:

Attack is a variable I set up as False in the Create Event.

Attack = keyboard_check_pressed(ord"Z"))

Code:
if Attack
{
sprite_index = spr_rangedattack;

if sprite_index = spr_rangedattack
{
with(instance_create(x,y,obj_bullet))
   {
       image_xscale = other.image_xscale;
       obj_bullet.speed = 15
       with(instance_place(x,y,obj_enemy))

     //Below checks if it is hitting an enemy
        {
         if(hit==0)
         {
            hit = 1;
            vsp = -3;
            hsp = sign(x - other.x)*4;
            image_xscale = sign(hsp)
         }     


      } 

  }


}
}
The code above seem to only let the projectile travel to the right, if the bullet is facing left it still travels to the right.

I am fairly certain that the speed, direction of the bullet and the bullet itself should be generated through the player. Although, I am not entirely sure how to go about it.


How would one go about executing this?

As always, looking forward to your responses.
 

FrostyCat

Redemption Seeker
First, you are setting the speed but not the direction. If you've got image_xscale already, you may as well do something with it.
Code:
with (instance_create(x, y, obj_bullet))
{
  image_xscale = other.image_xscale;
  hspeed = sign(image_xscale)*15;
}
Second, the enemy collision code is in the wrong place. That should be checked constantly by being in the bullet's Step code, not once by being here.
 
T

tserek

Guest
You just need to keep track which direction your character is facing. You could use a variable called dir to track it.

Create:
Code:
dir = 1; // facing to east as default
Step:
Code:
attack = keyboard_check_pressed(ord("Z"));

// facing dir will be set either -1 or 1
if keyboard_check(vk_right) +(-keyboard_check(vk_left)) !=0
{
dir = keyboard_check(vk_right) +(-keyboard_check(vk_left));
image_xscale = dir;
}

// shoot
if attack
{
inst = instance_create(x,y,obj_bullet);
inst.hsp = dir *4;
inst.image_xscale = dir;
}
Bullet step:
Code:
x+=hsp;
 
F

Freedom2Fight

Guest
Thank you for helping me out. Because of the two of you, I was able to adjust the code to work as intended. However I am experiencing some problems because the code is getting a bit unwieldy. I think it is time to use state machines but thats for another matter.

Once again thank you for your time and assistance.
 
C

crazybloodmonkey

Guest
i having this same problem it's only shooting right expect i didn't code the shooting i just have
in obj_player
key press K
( create instance obj_bullet x = 0 y=-16 and it's relative)

in obj_bullet
a create event and in it
move fixed -->
speed 8 and not relative.

so any ideas oh how to fix that with drag and drop stuff or am i going to have to code everything in the a execute code to fix this problem lol
 

TheouAegis

Member
It only moves right because you set the speed and direction of the bullet inside the bullet itself and not inside the player. You need to have the player set the bullet Direction based on the player's own Direction. You may have to use the code action, because I don't know if you can even specify a Target instance to effect in Drag and Drop otherwise. I mean, there is a way, but you pretty much have to use gml no matter what as far as I can tell.
 
F

Freedom2Fight

Guest
@crazybloodmonkey

If you are using drag and drop then this video might help you:


Its made with an old version of GM but it should still work with a few adjustments.

Sooner or later you're going to need to use GML.
 
C

crazybloodmonkey

Guest
@crazybloodmonkey

If you are using drag and drop then this video might help you:


Its made with an old version of GM but it should still work with a few adjustments.

Sooner or later you're going to need to use GML.
yeah i've done some coding for character moment and gravity and stuff. i'm just trying to cut down on it because i've been making this game for over a year. well actually i work on the game for 4 months and then took like a 6 month break because majoring in computer science didn't work out so i lost all motivation to continue to work on the game. anyway i just need to finish this before summer ends so i can say i at least made one game so i'm just trying speed some things up i know i'm definitely going to have to use some GML for the enemies lol
 
C

crazybloodmonkey

Guest
lol okay so after many failed attempts of the drag and drop videos and a couple reddit threads i decided to just take the advice of the two people here that help you and well it worked i can now shoot both ways....................................but now my character doesn't face left when he walks left he sort moon walks lol i just can't win. i sure this is a really easy fix it's probably just 1 or 3 more lines of code. anyways anyone want to hook me up with the fix i would search it but i think i've been working on the game for like 2 hours or more and i'm sort of tired now so i 'm calling it a day. if not then i guess i'll just figure it out tomorrow by some other means

edit: never mind i figured it out just had to put a xscale -1
 
Last edited by a moderator:
Top