Grenade Damage Through Walls

When a grenade explodes the damage goes through walls. Does anyone know how I would go about preventing this? I'm not sure how to approach this issue.

The explosion checks for players in a radius and applies damage - there are no projectiles that could hit a wall and just be destroyed on impact before reaching the player. So I'm not sure if it's even possible?

Any advice would be great :slight_smile:

Here is the part of the grenade script that does the damage (this is from the build in grenade)

if canDamage then
            local displacement = player:GetWorldPosition() - center

            -- The farther the player from the blast the less damage that player takes
            local minDamage = EXPLOSION_DAMAGE_RANGE.x
            local maxDamage = EXPLOSION_DAMAGE_RANGE.y
            displacement.z = 0
            local t = (displacement).size / EXPLOSION_RADIUS
            local damageAmount = CoreMath.Lerp(maxDamage, minDamage, t)

            -- Create damage information
            local damage = Damage.New(damageAmount)
            damage.sourcePlayer = WEAPON.owner

            -- Apply damage to player
            player:ApplyDamage(damage)

            -- Create a direction at which the player is pushed away from the blast
            player:AddImpulse((displacement):GetNormalized() * player.mass * EXPLOSION_KNOCKBACK_SPEED)
        end

You can do this by performing a raycast from the grenade to the player. If the player is hit by the raycast, then they are within line of sight of the grenade and should take damage.

if canDamage then

    local displacement = player:GetWorldPosition() - center

    --Cast a ray from the grenade to the player's chest

    local startPos = center

    local endPos = player:GetWorldPosition() + player:GetWorldTransform():GetUpVector()*50

    --Perform the raycast

    local hit = World.Raycast(startPos, endPos)

    --If the player has hit, then damage them

    if(hit ~= nil and hit.other ~= nil and hit.other == player) then

        -- The farther the player from the blast the less damage that player takes

        local minDamage = EXPLOSION_DAMAGE_RANGE.x

        local maxDamage = EXPLOSION_DAMAGE_RANGE.y

        displacement.z = 0

        local t = (displacement).size / EXPLOSION_RADIUS

        local damageAmount = CoreMath.Lerp(maxDamage, minDamage, t)

        -- Create damage information

        local damage = Damage.New(damageAmount)

        damage.sourcePlayer = WEAPON.owner

        -- Apply damage to player

        player:ApplyDamage(damage)

        -- Create a direction at which the player is pushed away from the blast

        player:AddImpulse((displacement):GetNormalized() * player.mass * EXPLOSION_KNOCKBACK_SPEED) 

    end

end

Raycast Documentation can be found here:
World Documentation

Awesome! Thank you so much!