Having trouble with making character shoot a bullet lower when crouching.

S

supesdupes

Guest
I essentially have my character's movement down, but I am having trouble figuring out how to make it so that when the player holds down and crouches the bullet lowers accordingly to the player's shorter height.
I am also confused as to make it so that the bullet comes from the tip of the player's gun. Any and all help would be greatly appreciated.

Code:
/// player create event
image_speed = 0.3; //How quickly the frames are played in the game, at 0 it wont' animate

walkSpeed = 1.2; //The variable controlling how quickly the player walks
grv = 2;
vspd = 0;

can_shoot = false;
can_minjump = true;
is_crouching = false;

//Sprites
spriteStand = spr_player;
spriteWalking = spr_player_walking;
GML:
///player step event
right = keyboard_check(ord("D"));
left = keyboard_check(ord("A"));
down = keyboard_check_pressed(ord("S"));
down1 = keyboard_check_released(ord("S"));
jump = keyboard_check_pressed(vk_space);
shoot = keyboard_check_pressed(ord("J"));

var move = (right - left) * walkSpeed;
hMoveDir = right - left

yMove = vspd + grv;

//Movement
if (move != 0)
    {
    image_xscale = hMoveDir
    image_speed = walkSpeed / .75;
    sprite_index = spriteWalking;
    }
else
    {
    sprite_index = spriteStand;
    }

//Jumping
if (place_meeting(x,y+1,obj_block)) && jump
{
    yMove = -25
}

//Horizontal Collisions
if(place_meeting(x + move, y, obj_block))
{
    while(!place_meeting(x + sign(move), y, obj_block))
        x += sign(move);
    move = 0;
}

x += move;

//Vertical Collisions
if(place_meeting(x, y + yMove, obj_block))
{
    while(!place_meeting(x, y + sign(yMove), obj_block))
        y += sign(yMove);
    yMove = 0
}

y += yMove

//Shooting
if (shoot)
{
       // Create a bullet
       bullet = instance_create_depth(x, y, -1, obj_bullet);
       bullet.hspeed = image_xscale * 5;
      // Shooting
       can_shoot = true;

       alarm[0] = 60;
}

if (can_shoot == false)
{
    spriteStand = spr_player;
    spriteWalking = spr_player_walking;
}
else
{
    spriteStand = spr_player_shoot;
    spriteWalking = spr_player_walking_shoot;
}

//Crouching
if (down)
{
    is_crouching = true;
}

if (down1)
{
    is_crouching = false;
}

if (is_crouching == true)
{
    spriteStand = spr_player_crouching;
    if (can_shoot == true)
    {
        spriteStand = spr_player_crshoot
    }
}
 

Nidoking

Member
When you create the bullet, you set its x and y to the correct positions based on the position, facing direction, and sprite of the character.
 

ophelius

Member
I haven't looked too much, but one thing I noticed is your inefficient keyboard inputs for down:

down = keyboard_check_pressed(ord("S"));
down1 = keyboard_check_released(ord("S"));

You can simply have this:
down = keyboard_check(ord("S"));
...
so now all this:
Code:
//Crouching
if (down)
{
    is_crouching = true;
}

if (down1)
{
    is_crouching = false;
}

if (is_crouching == true)
{
    spriteStand = spr_player_crouching;
    if (can_shoot == true)
    {
        spriteStand = spr_player_crshoot
    }
}
can be replaced with:

Code:
//Crouching
is_crouching = down; //while down is pressed, this will be true

if (is_crouching == true)
{
    spriteStand = spr_player_crouching;
    if (can_shoot == true)
    {
        spriteStand = spr_player_crshoot
    }
}
or if you don't need the is_crouching variable later:
Code:
//Crouching

if (down)
{
    spriteStand = spr_player_crouching;
    if (can_shoot)
    {
        spriteStand = spr_player_crshoot
    }
}
 
S

supesdupes

Guest
I haven't looked too much, but one thing I noticed is your inefficient keyboard inputs for down:

down = keyboard_check_pressed(ord("S"));
down1 = keyboard_check_released(ord("S"));

You can simply have this:
down = keyboard_check(ord("S"));
...
so now all this:
Code:
//Crouching
if (down)
{
    is_crouching = true;
}

if (down1)
{
    is_crouching = false;
}

if (is_crouching == true)
{
    spriteStand = spr_player_crouching;
    if (can_shoot == true)
    {
        spriteStand = spr_player_crshoot
    }
}
can be replaced with:

Code:
//Crouching
is_crouching = down; //while down is pressed, this will be true

if (is_crouching == true)
{
    spriteStand = spr_player_crouching;
    if (can_shoot == true)
    {
        spriteStand = spr_player_crshoot
    }
}
or if you don't need the is_crouching variable later:
Code:
//Crouching

if (down)
{
    spriteStand = spr_player_crouching;
    if (can_shoot)
    {
        spriteStand = spr_player_crshoot
    }
}
Thank you! That's much better.
 
S

supesdupes

Guest
When you create the bullet, you set its x and y to the correct positions based on the position, facing direction, and sprite of the character.
I understand that but when I try to code for the bullet to move lower when the player is crouching it either creates two bullets (one normal and one lower) or only creates the original standing shot. Could you please elaborate more?
 

Pixel-Team

Master of Pixel-Fu
If you made your bullet spawn right where the x and y of your character is, it would likely be wrong. What you want is an offset, which will add several pixels in the x and y of your character's position, to make it appear like its coming out of the gun. Now you can either make the x offset be added to the player's x position if he is facing right, and subtracted from his x position if he is facing left. If you need a separate offset for the crouch, then do that as well. Now you can have a temporary offset that will either use the offset of the standing or crouching positions depending on whether your player is standing or crouching. Here's a rough example:

GML:
offset_standing[0] = 5; //number of x pixels from character's standing x position
offset_standing[1] = 20; //numner of y pixels from character's standing y position

offset_crouching[0] = 5; //number of x pixels from character's crouching x position
offset_crouching[1] = 10 //number of y pixels from characters crouching y position

offset = -1;

if (player_standing) {
    offset = offset_standing;
} else {
    offset = offset_crouching;
}

//spawn bullet
if (facing_right) {
    instance_create_depth(player.x + offset[0], player.y + offset[1], player.depth, obj_bullet);
} else {
    instance_create_depth(player.x - offset[0], player.y + offset[1], player.depth, obj_bullet);
}
Something like that.
 
Last edited:

Mk.2

Member
Code:
bulletX = image_xscale * 10; //distance from player's x origin
if (is_crouching) bulletY = 5; //distance from player's y origin
else bulletY = 0;

bullet = instance_create_depth(x+bulletX, y+bulletY, -1, obj_bullet);
 
S

supesdupes

Guest
Code:
bulletX = image_xscale * 10; //distance from player's x origin
if (is_crouching) bulletY = 5; //distance from player's y origin
else bulletY = 0;

bullet = instance_create_depth(x+bulletX, y+bulletY, -1, obj_bullet);
I modified that values for bulletY to be 0 and then -5, but this worked perfectly. Thank you!
 
S

supesdupes

Guest
If you made your bullet spawn right where the x and y of your character is, it would likely be wrong. What you want is an offset, which will add several pixels in the x and y of your character's position, to make it appear like its coming out of the gun. Now you can either make the x offset be added to the player's x position if he is facing right, and subtracted from his x position if he is facing left. If you need a separate offset for the crouch, then do that as well. Now you can have a temporary offset that will either use the offset of the standing or crouching positions depending on whether your player is standing or crouching. Here's a rough example:

GML:
offset_standing[0] = 5; //number of x pixels from character's standing x position
offset_standing[1] = 20; //numner of y pixels from character's standing y position

offset_crouching[0] = 5; //number of x pixels from character's crouching x position
offset_crouching[1] = 10 //number of y pixels from characters crouching y position

offset = -1;

if (player_standing) {
    offset = offset_standing;
} else {
    offset = offset_crouching;
}

//spawn bullet
if (facing_right) {
    instance_create_depth(player.x + offset[0], player.y + offset[1], player.depth, obj_bullet);
} else {
    instance_create_depth(player.x - offset[0], player.y + offset[1], player.depth, obj_bullet);
}
Something like that.
Okay, that makes much more sense. Thank you!
 
Top