# Hitscan – Game Maker Top-Down Shooter Tutorial Part 2

5
8

How to create a hitscan mechanic for your shooter in Game Maker. Hitscan can make your game feel faster and more intense.

Check out my beginner’s course at

Algorithm used in the video:
var contact_x = aimed_x
var contact_y = aimed_y
var percent_start = 0;
var percent_end = 1;
var distance_x = aimed_x – gun_x
var distance_y = aimed_y – gun_y
var iterations = ceil(log2(point_distance(gun_x, gun_y, aimed_x, aimed_y)))
repeat (iterations) {
var middle_way = (percent_end – percent_start) * 0.5 + percent_start
var end_x = distance_x * middle_way + gun_x
var end_y = distance_y * middle_way + gun_y
var start_x = distance_x * percent_start + gun_x
var start_y = distance_y * percent_start + gun_y
var found = collision_line(start_x, start_y, end_x, end_y, oTarget, true, true)
if (found == noone) {
percent_start = middle_way;
} else {
target = found
contact_x = end_x
contact_y = end_y
percent_end = middle_way
}
}

Nguồn: https://rozanighani.com/

Xem thêm bài viết khác: https://rozanighani.com/game/

1. Big thanks for the tutorial, this really helped! One question, however, is there a way to implement some kind of piercing variable, for example depending on a gun, the bullet could travel through a certain amount of enemies etc, or even special kind of walls?

2. With this system can't you just shoot through walls

3. This really helped me a lot, thanks!

4. Huh, it's an interesting implementation of binary searching.

5. The hit is a bit offset, as the gun is not directly aligned with the character. To fix it, add this between 2nd and 3rd line of the Left Clicked Event:
var gun_direction = point_direction(gun_x, gun_y, mouse_x, mouse_y)

Then use 'gun_direction' in the following two lines instead of 'image_angle':
var aimed_x = gun_x + lengthdir_x(500, gun_direction)
var aimed_y = gun_y + lengthdir_y(500, gun_direction)