Simple Weapon-Aiming and Shots

Hi Folks,

I`m an interested beginner in programming and using GMS2. I´ve already seen different
YT-Tutorials, f.e. making a Jump`N`Run game. I can follow all codes and steps. But I
hang up on a simple weapon behaviour. Maybe there is somebody can gimme a hint!?

I have a fully functional character with eight dirs over keyboard_check(vk_X).
But I don´t wanna use mouse (point_direction) for aiming with my weapon.
I just wanna use oGun and oBullet (each in a seperate layer, devided from oPlayer),
but following the oPlayer (x = oPlayer.x; and y = oPlayer.y;). Now I just want to aim
and shot to the right, when oPlayer is looking right by last keypress). And aiming
and shooting to the left, when oPlayer is looking to the left by last keypress).
(Have a look down for the concerning code)

Hope I could explain my problem, well!?
Thankful for each hint.
Best wishes,

Architeuthis o_O

Current code:

/// @desc Gun-Behaviour

x = oPlayer.x;
y = oPlayer.y;
if (keyboard_check(vk_left))
{
image_xscale = -1;
}
if (keyboard_check(vk_right))
{
image_xscale = 1;
}
firingdelay = firingdelay -1;
recoil = max(0,recoil - 1);
if (keyboard_check(vk_up)) && (firingdelay < 0)
{
recoil = 2;
firingdelay = 5;
with (instance_create_layer(x,y,"Drop",oDrop))
{
speed = 30;
direction = other.image_angle + random_range (-4,4);
}
}

 
Last edited:
you can use your image_xscale to know which direction you are pointing in
(1) means it's pointing right
(-1) means it pointing left
you can also use it to calculate a distance on either side
Code:
var dir=image_xscale*32; // this will either be 32(32*1) or -32(32*-1) EDIT: sorry for the typo
var drop=instance_create_layer(x+dir,y,"Drop",oDrop); // this will store the new instance id in the variable 'drop'
if(dir>0)  // this gives drop a direction to move in
  drop.direction=0;
else
  drop.direction=180;
drop.speed=30; and here we give that drop some speed to move in that direction
EDIT: typoes and functions are properly displayed
 
Last edited:
Hey Mr. Tudor.

Thank you for your hint!
I try to find out, how does it work.
I understand the logic and the script of your code.
But it shows a "unknown function" Error in the var drop - row.
The best way to learn is to eliminate step by step the problems by finding out the solutions.
I try to integrate your part and fit it to my code. The solution must be there.
Thank you for your help so far! :)

Best wishes.
Archi
 

Slyddar

Member
But it shows a "unknown function" Error in the var drop - row.
instance_create needs to be instance_create_layer

For future, a way for you to work this out would be that Unknown function indicates the function used is not known. instance_create should of turned an instance variable colour instead of a function colour, so you would know it's wrong. Might help you next time there's a problem.
 
Hi´s guys! :)

I´ve noticed it immediately, too!
I knew what Mr. Tudor meant. But despite of that it doesn´t work out, as I would like to have.
The variables of speed and recoil were already set in oGun (Event/Create Event/Create).
I try to weave Mr. Tudors hints into the previously shown code (Step). I absolutely can
follow the code. But somewere there is still a problem with shooting into the walk direction.
The drops of the watergun spill in broken shapes, irregularly and into wrong direction.
I guess there is a conflict between your hint and my code. I fear I insert it wrong!

I keep on finding out. But I am thankful for further hints, if you like to help a beginner
with very big interest and irony will! :rolleyes:

Have a nice and productive day.
Archi
 
Hi Sly!

I just want to have a simple left/right-movement with a xscaling (aiming/shooting to the left and right) of "oGun".

Here the current code, you asked for:



// These are the variables, set within object "oGun" (Create)

firingdelay = 0;
recoil = 0;

---------------------------------------------------------------------------

// This is the code of object "oGun" (Step)

// oGun-dragging like oPlayer movement

x = oPlayer.x;
y = oPlayer.y;

// Weapon mirroring = walk-direction

if (keyboard_check(vk_left))
{
image_xscale = -1;
}
if (keyboard_check(vk_right))
{
image_xscale = 1;
}
// Shot and weapon behaviour
firingdelay = firingdelay -1;
recoil = max(0,recoil - 1);
}
// Influenting shoot-direction
if (keyboard_check(vk_up)) && (firingdelay < 0)
{
recoil = 2;
firingdelay = 5;
var dir=image_angle*32;
var drop=instance_create_layer(x+dir,y,"drop",oDrop);
{
direction = other.image_angle + random_range (-4,4);
if(dir>0)
{
drop.direction=0;
}
else
{
drop.direction=180;
}

image_angle = direction;
drop.speed=10;
}
}



END OF CODE



Can you see, what I can´t see?
Thaks a lot for your help.

Best wishes,
Archi
 

Slyddar

Member
On this forum, when posting, if you click the little icon next to the disc, fourth from right, you can select code and paste your code in a more readable format.

I believe your problem is Tudor had written image_angle instead of image_xscale. Try this now:

Code:
// oGun-dragging like oPlayer movement
x = oPlayer.x;
y = oPlayer.y;

// Weapon mirroring = walk-direction
if (keyboard_check(vk_left))
{
    image_xscale = -1;
}
if (keyboard_check(vk_right))
{
    image_xscale = 1;
}
// Shot and weapon behaviour
firingdelay = firingdelay -1;
recoil = max(0,recoil - 1);

// Influenting shoot-direction
if (keyboard_check(vk_up)) && (firingdelay < 0)
{
    recoil = 2;
    firingdelay = 5;
    var dir=image_xscale*32;
    var drop=instance_create_layer(x+dir,y,"drop",oDrop);
    {
        direction = image_xscale + random_range (-4,4);
        if(dir>0)
        {
            drop.direction=0;
        }
        else
        {
            drop.direction=180;
        }
        image_angle = direction;
        drop.speed=10;
    }
}
 
Oh, thanks for that info... next time I´ll put codes with that icon onto that screen. :)
And a far bigger THANK YOU! for the code you gave me. I try it at once and I report.
 
I believe your problem is Tudor had written image_angle instead of image_xscale. Try this now:
Wow I am full of mistakes on this post... sorry for the confusion of image_angle and image_xscale. I totally flubbed it on that one too.

PS: For seeing absolutely clear: What is *32 meant for?
the 32 is an arbitrary number we put in to give it 32 pixels distance away from object. You can change that to any positive number you want. If you put in 5 then it will be either 5 pixels away to the right or -5 pixels away to the left. Depending on the value of image_xscale. The number itself is just how far from object do you want to create the "drop"
 
I´m sorry. Mr. Tudor! :eek:

Haaa... the Sly is right!
I wrote you, too. But I obviously forgot to post the reply. :(
I deeply thank you for your help! I hope you don´t get my silence wrong!?
Often am a nutty deer! :D

And thanks for the info about *32.
Now I can see the connection.
Hope I can help you too... one day... with something... of my thin wisedom.

Greeetings!
Archi
 
Top