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

[Resolved] Problems with player ducking

pixeltroid

Member
I have a code that gets my player sprite to change to his ducking position...but there are several problems

1. Pressing the walk Left or right buttons causes him to slide on the ground in his crouching pose.
2. I am unable to shoot weapons that work when the shoot button is pressed down
3. I can shoot weapons that work when the shoot button is tapped... but the bullets origin point is the same as it is when he stands up so it looks as if the bullets are coming from the players head.

Here's my code for ducking in players step event

//set to ducking
if keyboard_check(ord("S"))
{ ducking = true; }
else
{ ducking = false; }

//check to see if player on ground
if(place_meeting(x,y+1,obj_solid))
{
vspeed = 0;
grounded = true;
if(ducking)
{ hspeed = 0; }

if keyboard_check(ord("W"))
{ vspeed = -10; }


//display proper animation when on ground

if(grounded)
{
if(hspeed != 0)
{
sprite_index = spr_playerstand;
image_speed = .2;
}
else if(ducking)
{sprite_index = spr_playerduck;
hspeed = 0

}
}
}
Here's the keypress function for the shoot button that needs to be pressed down

//original
if (state == 'move' && canshoot = true && health > 0) {
audio_play_sound(sfx_herolaser,10,false)
image_speed = .3;
sprite_index = spr_playershoot;
state = 'playershoot' ;
}
the script for players shoot state:

{
{
if (bullets > 2 && image_index >1) {
//create bullet
b=instance_create(x+4,y-4,obj_bullet);
bullets -= 1;
b.speed = 6;

//check direction
if (image_xscale > 0) {
b.direction = 0;
} else {
b.direction = 180;
}
}
Can someone go through my codes and tell me how to modify them to achieve:

a) Not being able to move when I'm crouching.
b) Being able to shoot when the shoot button is pressed down.
c) Have the bullets origin lowered by a few pixels in crouching mode.

Any and all help will be appreciated.
 

Seryal

Member
a) You have your "else if (ducking)" statement positioned to where you would only be able to duck if hspeed == 0 and you are displaying your spr_playerstand when the player is moving.
Code:
if(hspeed != 0) // If player is move left or right
{
sprite_index = spr_playerstand; // Show the player standing
image_speed = .2;
}
else if(ducking) // Or if the player is not moving left or right
{sprite_index = spr_playerduck; // Show the player ducking.
hspeed = 0

}
b) What is your shoot button?

c) Have the bullets origin lowered by a few pixels in crouching mode.
Code:
if (ducking == true) {
     b=instance_create(x+4,y-PositionWhenDucked,obj_bullet);
} else {
     b=instance_create(x+4,y-4,obj_bullet);
}

Some of my ducking code I've used that works:

Code:
if (move!=0) {
            sprite_index = spr_player_run;
        } else {
            if (key_s) {
                sprite_index = spr_player_duck;
            } else {
                sprite_index = spr_player_standing;
            }
        }
 

pixeltroid

Member
a) You have your "else if (ducking)" statement positioned to where you would only be able to duck if hspeed == 0 and you are displaying your spr_playerstand when the player is moving.
Code:
if(hspeed != 0) // If player is move left or right
{
sprite_index = spr_playerstand; // Show the player standing
image_speed = .2;
}
else if(ducking) // Or if the player is not moving left or right
{sprite_index = spr_playerduck; // Show the player ducking.
hspeed = 0

}
b) What is your shoot button?

c) Have the bullets origin lowered by a few pixels in crouching mode.
Code:
if (ducking == true) {
     b=instance_create(x+4,y-PositionWhenDucked,obj_bullet);
} else {
     b=instance_create(x+4,y-4,obj_bullet);
}

Some of my ducking code I've used that works:

Code:
if (move!=0) {
            sprite_index = spr_player_run;
        } else {
            if (key_s) {
                sprite_index = spr_player_duck;
            } else {
                sprite_index = spr_player_standing;
            }
        }
Hi. My shoot button that needs to be pressed down is 'O'. This doesnt work with Im ducking.

The P button that needs to be tapped works when Im ducking.

I've now managed to lower the bullet origin lowered using the if else statement. Just toggling now to get the bullets match the barrel of the gun now.

Will try out your method for ducking. Thanks.
 

Seryal

Member
I'm sorry, I just realize my code with not allow you to duck and move at the same time. If you want to be able to do that try moving my code around like this:

Code:
if (move!=0) {
            if (key_s) {
                sprite_index = spr_player_duck;
            } else {
                sprite_index = spr_player_run;
            }
        } else {
                sprite_index = spr_player_standing;
        }
If you want feel free to PM me you project file zipped up and so I can look at everything and I can try to work something out and leave a lot of notes and send it back.
 

pixeltroid

Member
a) You have your "else if (ducking)" statement positioned to where you would only be able to duck if hspeed == 0 and you are displaying your spr_playerstand when the player is moving.
Code:
if(hspeed != 0) // If player is move left or right
{
sprite_index = spr_playerstand; // Show the player standing
image_speed = .2;
}
else if(ducking) // Or if the player is not moving left or right
{sprite_index = spr_playerduck; // Show the player ducking.
hspeed = 0

}
I didnt understand this part. My player ducks when I press the duck button, but if I press the left or right button he starts moving. How do I make it such that pressing left and right ( A and D) while ducking does nothing?
 

Seryal

Member
Ignore my previous post code. I thought you wanted to be able to move while ducking.

I would define a variable for your ducking key.
Code:
globalvar duck_key;
duck_key = keyboard_check(ord("S"));
Then, where ever you have your moving left and right code, I would add an "&&" operation. E.G.
Code:
globalvar key_d;
globalvar key_a;
key_d = keyboard_check(ord("D"));
key_a = keyboard_check(ord("A"));

if (key_d && !duck_key) {     // If user presses D AND is NOT holding S
     x += 1; // Move the player right.
} else if (key_s && !duck_key) {     // If user presses S AND is NOT holding S
     x -= 1; // Move the player left.
}

Or better yet

Code:
globalvar key_d;
globalvar key_a;
key_d = keyboard_check(ord("D"));
key_a = -keyboard_check(ord("A"));

move = key_d + key_a; // Move is either 1, -1, or 0;

if (move != 0 && !duck_key) {    
     x += move;
}
 
Last edited:

pixeltroid

Member
Ignore my previous post code. I thought you wanted to be able to move while ducking.

I would define a variable for your ducking key.
Code:
globalvar duck_key;
duck_key = keyboard_check(ord("S"));
Then, where ever you have your moving left and right code, I would add an "&&" operation. E.G.
Code:
globalvar key_d;
globalvar key_a;
key_d = keyboard_check(ord("D"));
key_a = keyboard_check(ord("A"));

if (key_d && !duck_key) {     // If user presses D AND is NOT holding S
     x += 1; // Move the player right.
} else if (key_s && !duck_key) {     // If user presses S AND is NOT holding S
     x -= 1; // Move the player left.
}

Or better yet

Code:
globalvar key_d;
globalvar key_a;
key_d = keyboard_check(ord("D"));
key_a = -keyboard_check(ord("A"));

move = key_d + key_a; // Move is either 1, -1, or 0;

if (move != 0 && !duck_key) {   
     x += move;
}
I appreciate all your help....but this is still causing my player to move while ducking. I just want him to be locked in position when the S button is held down. Meaning pressing A and D should have no effect
 

Yal

šŸ§ *penguin noises*
GMC Elder
This would be much easier to solve with a state machine...
Code:
//create event
state = "stand"
/*all your existing code*/

//step event
switch(state){
  case "stand":
    /*your original code*/
    if(keyboard_check(ord('S'))){
      state = "duck"
    }
    break

    case "duck":
      /*adapted version of your original code, doesn't let you move and will shoot from a different position than when standing*/
    if not(keyboard_check(ord('S'))){
      state = "stand"
    }
    break
}
 

Seryal

Member
I appreciate all your help....but this is still causing my player to move while ducking. I just want him to be locked in position when the S button is held down. Meaning pressing A and D should have no effect
As Yal said above, a state machine would help to alleviate your issue. I've only ever used them for A.I. but it will work for player states too of course.

As far as my method goes.. like I said all you have to do is put "&& !key_duck" in whatever if statement you are using to initially move your player left and right and that should work fine..
 

pixeltroid

Member
As Yal said above, a state machine would help to alleviate your issue. I've only ever used them for A.I. but it will work for player states too of course.

As far as my method goes.. like I said all you have to do is put "&& !key_duck" in whatever if statement you are using to initially move your player left and right and that should work fine..
Hey I did it. And it works. I misunderstood earlier...

I added "&&!key_duck" in my original code to move left and right....and now my player is locked in position when he is ducking. Thanks!

Now 2 of my three problems are solved. I just need to be able to shoot when Im holding down the O button while shooting.

Any idea?
 
Last edited:

Seryal

Member
Something like this?

This code would go into something like your gun object step event.

Code:
if(keyboard_check(ord('O'))) {
        if (bullets <= 0) {
                audio_play_sound(snd_OutOfAmmoClick,0,0); // OUT OF AMMO CLICK SOUND
        }
        if (bullets > 0) {      
                 if (obj_player.ducking == true) {
                        b=instance_create(x+4,y-PositionWhenDucked,obj_bullet);
                    } else {
                        b=instance_create(x+4,y-4,obj_bullet);
                    }
                    b.speed = 6;
                    b.direction = image_angle + random_range(-1,1); // Remove this line if you do not want your bullets to spread.
                    b.image_angle = image_angle;  // Set bullet angle to that of your gun.
                    audio_play_sound(snd_shoot,0,0); // FIRE WEAPON SOUND
                }
                // Use bullets
                bullets -= 1;
        }

}
 

pixeltroid

Member
Something like this?

This code would go into something like your gun object step event.

Code:
if(keyboard_check(ord('O'))) {
        if (bullets <= 0) {
                audio_play_sound(snd_OutOfAmmoClick,0,0); // OUT OF AMMO CLICK SOUND
        }
        if (bullets > 0) {     
                 if (obj_player.ducking == true) {
                        b=instance_create(x+4,y-PositionWhenDucked,obj_bullet);
                    } else {
                        b=instance_create(x+4,y-4,obj_bullet);
                    }
                    b.speed = 6;
                    b.direction = image_angle + random_range(-1,1); // Remove this line if you do not want your bullets to spread.
                    b.image_angle = image_angle;  // Set bullet angle to that of your gun.
                    audio_play_sound(snd_shoot,0,0); // FIRE WEAPON SOUND
                }
                // Use bullets
                bullets -= 1;
        }

}
thanks man. I'll give this a shot.
 

pixeltroid

Member
Let me know if you run into any problems.
thanks man I will. I appreciate all the help I get here.

Ive been on GM all day. Feeling a little fatigued. Heading out to the pub now to relax for a while!

Will implement the code when I get back later.
 

pixeltroid

Member
@Seryal

Your code worked to an extent.
However, the rate of fire when I shoot while crouching is far more than when I shoot standing up. The bullets come out like a beam when I shoot while crouching. Any idea how to remedy this?
 

Seryal

Member
Yes I could fix the problem but I need to see more code. I would suggest PMing me your project so I can see everything and then I will make a video or a log on what changes need to happen to fix your rapid-fire issue.

I imagine its because you must be using a timer/alarm/counter for firing when ducking = false, but I could help more if I had more information on how you have the system set up.
 

pixeltroid

Member
Yes I could fix the problem but I need to see more code. I would suggest PMing me your project so I can see everything and then I will make a video or a log on what changes need to happen to fix your rapid-fire issue.

I imagine its because you must be using a timer/alarm/counter for firing when ducking = false, but I could help more if I had more information on how you have the system set up.
I'll send you my project file. I'm on phone at work now. Will send it you when I get back home!
 

Seryal

Member
Okay, I'm going to be going to sleep soon, I've been awake for a couple days now so it's about time to take a nap. I will check your project out when I wake up!
 

pixeltroid

Member
Okay, I'm going to be going to sleep soon, I've been awake for a couple days now so it's about time to take a nap. I will check your project out when I wake up!
hey here's the playermovement project file

https://drive.google.com/open?id=1zA4-zXwe1hosNUO4gL6_M3V5PkxxRzXb

Controls:
A and D -- Move left and right
W -- Jump
S -- Crouch
O -- shoot bullets (this is causing a problem)
P -- Use alternate weapon

The bullet rate when crouching and facing left is what the ideal bullet rate should be. But player shoots at different rates when he is facing right or facing left while standing.

Please look into the code and tell me how to fix it! Thanks!
 

Seryal

Member
Code:
--CHANGELOG--
obj_player Step 3 Line 26 - commented out
obj_player Step 3 Line 27 - commented out
obj_player Step 3 Line 36 - commented out (you don't need to check if grounded because this code is already inside of if(place_meeting(x,y+1,obj_solid))
scr_playershoot_state Line 1 - commented out
scr_playershoot_state Line 23 - commented out (you don't need to check if the O is being pressed because this code only runs when the O is being pressed.
scr_playershoot_state Line 21 - Subtracting bullets moved to the last line of the script outside of any conditions.
scr_playershoot_state Lines 6-10 & 14-18 - Checking direction for both states of either ducking or not ducking.
scr_player_move_state Line 10 - commented out (unnecessary brackets)
scr_player_move_state Line 64 - commented out (unnecessary brackets)
scr_player_move_state Line 6 - Added key_d definition
scr_player_move_state Line 7 - Added key_a definition
scr_player_move_state Line 13 - Used key definitions help make it easier to read.
scr_player_move_state Line 29 - Using separate if statement instead of an else if.
scr_player_move_state Line 14 - commented out (unnecessary code)
scr_player_move_state Line 22-28 - commented out (unnecessary code)
scr_player_move_state Line 31 - commented out (unnecessary code)
scr_player_move_state Line 39-45 - commented out (unnecessary code)
scr_player_move_state Line 46 - using if statement, instead of an else
Since you are resetting bullets to 3 at the end of the animation. It was firing faster when you are ducking because there is only 1 image in that sprite.
I've copy and pasted the image to make it the same number as the shooting sprite and this helps; however the player will sometimes shoot two bullets in rapid succession.
obj_player Animation End Line 5 - commented out
Added obj_player Alarm [1]
obj_player Create Line 7 - alarm[1] = (room_speed / 3);
scr_playershoot_state Line 22 - if (alarm[1] == -1) alarm[1] = (room_speed / 3);

You can change the alarm[1] to change the rate of fire.
https://drive.google.com/open?id=1_NLxY4k9tO5mSL58xiks5TghfdnI2rjs
 

pixeltroid

Member
Code:
--CHANGELOG--
obj_player Step 3 Line 26 - commented out
obj_player Step 3 Line 27 - commented out
obj_player Step 3 Line 36 - commented out (you don't need to check if grounded because this code is already inside of if(place_meeting(x,y+1,obj_solid))
scr_playershoot_state Line 1 - commented out
scr_playershoot_state Line 23 - commented out (you don't need to check if the O is being pressed because this code only runs when the O is being pressed.
scr_playershoot_state Line 21 - Subtracting bullets moved to the last line of the script outside of any conditions.
scr_playershoot_state Lines 6-10 & 14-18 - Checking direction for both states of either ducking or not ducking.
scr_player_move_state Line 10 - commented out (unnecessary brackets)
scr_player_move_state Line 64 - commented out (unnecessary brackets)
scr_player_move_state Line 6 - Added key_d definition
scr_player_move_state Line 7 - Added key_a definition
scr_player_move_state Line 13 - Used key definitions help make it easier to read.
scr_player_move_state Line 29 - Using separate if statement instead of an else if.
scr_player_move_state Line 14 - commented out (unnecessary code)
scr_player_move_state Line 22-28 - commented out (unnecessary code)
scr_player_move_state Line 31 - commented out (unnecessary code)
scr_player_move_state Line 39-45 - commented out (unnecessary code)
scr_player_move_state Line 46 - using if statement, instead of an else
Since you are resetting bullets to 3 at the end of the animation. It was firing faster when you are ducking because there is only 1 image in that sprite.
I've copy and pasted the image to make it the same number as the shooting sprite and this helps; however the player will sometimes shoot two bullets in rapid succession.
obj_player Animation End Line 5 - commented out
Added obj_player Alarm [1]
obj_player Create Line 7 - alarm[1] = (room_speed / 3);
scr_playershoot_state Line 22 - if (alarm[1] == -1) alarm[1] = (room_speed / 3);

You can change the alarm[1] to change the rate of fire.
https://drive.google.com/open?id=1_NLxY4k9tO5mSL58xiks5TghfdnI2rjs
awesome. Thanks a lot man.

My player movement code is very messy because most of it was collected from various youtube tutorials. I'll begin to implement these changes now!

Thanks again!
 

pixeltroid

Member
@Seryal

I implemented your codes into my main project file. I tweaked the bullet rate and its looking good now. Thanks. I'm going to take a break from code and do some graphics for a while.

Thanks again!
 
Last edited:
Top