Forum

> > CS2D > Scripts > zombies knockback pr‎oblem
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch zombies knockback pr‎oblem

3 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt zombies knockback pr‎oblem

muslim
User Off Offline

Zitieren
I know there are a lot of threads that talk about zombie knockback, but I still couldn't figure out why zombies still get stuck even though I'm checking the walkable area.

They don't always get stuck though. Sometimes it happens, but it's still annoying and ruins the game.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function _knockback(id,source)
    local shooter_x,shooter_y = player(source,"x"),player(source,"y")
    local zombie_x,zombie_y = player(id,"x"),player(id,"y")
    local direction_x,direction_y = zombie_x - shooter_x,zombie_y - shooter_y

    local length = math.sqrt(direction_x * direction_x + direction_y * direction_y)
    direction_x,direction_y = direction_x / length,direction_y / length

    local new_x,new_y = zombie_x + dataText[id].knockback * direction_x,zombie_y + dataText[id].knockback * direction_y
    local tile_x,tile_y = math.floor(new_x/32),math.floor(new_y/32)

    if tile(tile_x,tile_y,"walkable") then
        parse("setpos "..id.." "..new_x.." "..new_y)
    end
end

alt Re: zombies knockback pr‎oblem

Gaios
Reviewer Off Offline

Zitieren
You don't. The knockback value could be smaller than the zombie hitbox size. Also the way you check it is 1D dimension and you need 2D with 20x20 square mask. The thing you should do is ray casting.

alt Re: zombies knockback pr‎oblem

Gaios
Reviewer Off Offline

Zitieren
user muslim hat geschrieben
@user Gaios: I didn't understand...

Man...

The issue lies in the way you're checking for walkable areas. Currently, you're using a 1D check, which may not accurately account for the 2D nature of the problem. To overcome this, there are a couple of approaches you can consider.

One option is to implement ray casting. This involves projecting a ray from the zombie's current position towards its new position after knockback. By iterating over the tiles along the ray's path, you can check for collisions with non-walkable tiles. This method could be the best.

Another alternative is to use a primitive collision check. In the code snippet provided below, I've outlined an example implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function collides(tile_x, tile_y)
    local box_size = 20
    local corner1_x, corner1_y = tile_x - box_size/2, tile_y - box_size/2
    local corner2_x, corner2_y = tile_x + box_size/2, tile_y - box_size/2
    local corner3_x, corner3_y = tile_x + box_size/2, tile_y + box_size/2
    local corner4_x, corner4_y = tile_x - box_size/2, tile_y + box_size/2

    if tile(corner1_x, corner1_y, "walkable") and
       tile(corner2_x, corner2_y, "walkable") and
       tile(corner3_x, corner3_y, "walkable") and
       tile(corner4_x, corner4_y, "walkable") then
        return false
    end

    return true
end

function _knockback(id, source)
    local shooter_x, shooter_y = player(source, "x"), player(source, "y")
    local zombie_x, zombie_y = player(id, "x"), player(id, "y")
    local direction_x, direction_y = zombie_x - shooter_x, zombie_y - shooter_y

    local length = math.sqrt(direction_x * direction_x + direction_y * direction_y)
    direction_x, direction_y = direction_x / length, direction_y / length

    local new_x, new_y = zombie_x + dataText[id].knockback * direction_x, zombie_y + dataText[id].knockback * direction_y

    if not collides(new_x, new_y) then
        parse("setpos " .. id .. " " .. new_x .. " " .. new_y)
    end
end
In this updated code, the
collides
function checks for collisions between the zombie's new position and the walkable tiles. It creates a square box around the target position (20x20 pixels) and verifies that none of the box's corners overlap with non-walkable tiles. If there is a collision, the zombie's position remains unchanged.
3× editiert, zuletzt 19.06.23 15:30:04
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht