Search code examples
game-maker-studio-2

game maker studio 2: bullets don't move to the mouse direction


I'm trying to make shooting mechanics in my game, but I can not force bullets move to the mouse direction. It just creating under player object and stay on his coordinates . Uses physics don't change situation, no matter obj_bullet just "uses physics"(and with sensor parameter as well) or not uses.

They just stay on coordinates where I created them

x

When I don't use physics on obj_bullet, bullet sprite rotating depends of mouse position but still don't move.

xx

I wrote a simple code for my bullets in "create" event of bullet

BulletPower=1;
bulletSpeed=10;
speed=bulletSpeed;
direction=point_direction(x,y,mouse_x, mouse_y);
direction+=random_range(-5,5);
image_angle=direction;

Tried to use move_towards_point(mouse_x, mouse_y,bulletSpeed); instead but doesn't work

Then I've made instance layer "Bullets" above all others and then made condition in "step" event of obj_player

if (mouse_check_button(mb_left)){
    instance_create_layer(x,y,"Bullet",obj_bullet);
}

And bullets just drop on player coordinates. I had theory that bullet just stack in player, but even if I create bullet not on player coordinates it still doesn't work Also I changed axis of player and bullet sprites but still no result...

I really don't understand why it doesn't work, because I saw almost same code on youtube tutorials like this and in their case it works.


Solution

  • I solved this problem, I hope it help somebody. Bullets don't move because variable "speed" will not work if room using physics. You need make obj_bullet as physic object(uses physics),then write code in "create" event

    direction=point_direction(x,y, mouse_x , mouse_y );
    phy_rotation = -direction;//it's like image angle for bullets
    

    and make event "step" event

    phy_position_x += lengthdir_x(bulletSpeed, direction);
    phy_position_y += lengthdir_y(bulletSpeed, direction);
    

    Done! Now bullets will move.