GameMaker Instance direction help!

P

Pikpik

Guest
So I have a shooting ability, but the projectiles only go to the right. I want the bullets to go the direction the player is facing when they are shot out.

Here's the code I have currently:
GML:
//shoot
if keyboard_check(ord("X"))
{
    sprite_index = spr_player_shoot;
    image_index = 0
}
firingdelay = firingdelay - 1;
if keyboard_check(ord("X")) && (firingdelay < 0)
{
    firingdelay = 10;
    with (instance_create_layer(x+15,y+23,"Instances",obj_projectile))
    {
        speed = 10;
        direction = other.image_xscale;
        image_xscale = direction;
    }
}
I got most of it from this video:

 

woods

Member
your bullet has direction = other.image_xscale;
im guessing you are making a platformer and using image_xscale of 1 and -1 to change the direction of the player....

image_xscale simply flips the sprite.. it doesnt change the direction of the object.
direction would equal 1 or -1

im guessing your bullet is going ever so slightly up or down from flat right...


you need to define/reference the obj_player direction for your bullet code to work correctly ;o)
 
P

Pikpik

Guest
is this how you do it?

GML:
if image_xscale = -1
{
    facingright = false
}
else
{
    facingright = true
}
GML:
if keyboard_check(ord("X")) && (firingdelay < 0)
{
    firingdelay = 10;
    with (instance_create_layer(x+15,y+23,"Instances",obj_projectile))
    {
        if facingright = true
        {
            speed = 10;
            direction = other.image_xscale;
            image_xscale = direction;
        }
        if facingright = false
        {
            speed = -10;
            direction = other.image_xscale;
            image_xscale = direction;
        }
    }
}
 

woods

Member
you are on the right track with using a variable to hold the facing.. but you're still not finding the variable "direction" of the player ;o)


All instances in GameMaker: Studio have certain "built in" properties that you can use and set to govern how they look and behave. Direction is one of those properties and can be used to set the direction of movement of the instance when the instance has a speed other than 0. Note that directions in GameMaker: Studio are usually calculated as 0° being right, 90° being up, 180° being left and 270° being down.


since you are dealing with left and right only.. you can use 0 and 180 for your bullet's direction.

maybe something along these lines...

GML:
if facingright = true
        {
            speed = 10;
            direction = 0;
        }
if facingright = false
        {
            speed = -10;
            direction = 180;
        }
 
P

Pikpik

Guest
I get this error when I try to shoot:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_player:

Variable obj_projectile.facingright(100008, -2147483648) not set before reading it.
at gml_Object_obj_player_Step_0 (line 124) - if facingright = true
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_player_Step_0 (line 124)
 

woods

Member
edit:
are you setting the variable in the create event of the player?
facingright = true;




sorry, my mistake..
we need to reference the PLAYER variable from the bullet... so we use the dot notation ;o)


if obj_player.facingright = true
{
speed = 10;
direction = 0;
}
if obj_player.facingright = false
{
speed = -10;
direction = 180;
}
 

Nidoking

Member
Variable obj_projectile.facingright was not set before reading it in obj_player Step event. You should fix that.
 

Mk.2

Member
GML:
if facingright = true
        {
            speed = 10;
            direction = 0;
        }
if facingright = false
        {
            speed = -10;
            direction = 180;
        }
These will put the bullet in the same direction.

Just use:

GML:
with (instance_create_layer(x+15,y+23,"Instances",obj_projectile))
{
    speed = 10 * other.image_xscale;
    image_xscale = other.image_xscale;
}
 
Top