Grenade can be thrown through wall

I have not finished the complete course, maybe this is adressed later?!?
Seems to be a complex problem to me…

1 Like

I wanted to enable you to throw the grenade directly on top of a Crate to blow it up but it does have that one side effect.
One simple solution is to add a Pathfinding check just like in the Move action, if there’s no path to the target grid position or the path is too long then don’t validate it.
Alternatively fire a Raycast just like when validating the Shoot action, if there’s a wall in front don’t validate that grid position.

Hey, I’ve been trying to implement what you suggested because I really don’t want the grenades to fly through walls. But I don’t quite get what you mean when saying that you can use the pathfinding option. Couldn’t with that option it still go behind walls since the path still would be valid just behind walls? great course btw

Edit: i have managed to implement it so that it checks for the units and then validates if there is an obstacle inbetween. I’m just struggling with how to check any grid and not just the units since i also want that the grenade can be thrown onto a grid without a unit

Im not CM but i would do it like this: First check, if there is a path. If there is no path or path is too long, then disallow.
Second, if there is a path, make a raycast from source to targetposition. If its blocked by wall, disallow.
(Look in ShootAction).
Guess, the easiest way is, making a raycast and skip the pathfinding.

yeah, seems smart. I have now down this and it seems to be working.

            Vector3 gridWorldPosition = LevelGrid.Instance.GetWorldPosition(testGridPosition);
            Debug.Log(gridWorldPosition);

            Vector3 unitWorldPosition = LevelGrid.Instance.GetWorldPosition(unitGridPosition);
            Vector3 shootDir = (gridWorldPosition - unitWorldPosition).normalized;

            float unitShoulderHeight = 1.7f;
            if (Physics.Raycast(
                unitWorldPosition + Vector3.up * unitShoulderHeight,
                shootDir,
                Vector3.Distance(unitWorldPosition, gridWorldPosition),
                obstaclesLayerMask))
            {
                continue;
            }
1 Like

What exactly are you struggling with? The validation logic already cycles through all grid positions, you can apply whatever validation logic you want on each of those grid positions. Maybe do a Raycast and skip if there’s a collider in front, maybe ask the pathfinding system if that grid position is unwalkable and skip it as well, etc.

I think you’re better off not using the pathfinding for this.
If there is a wall in between, a Raycast can invalidate.
But if there is a hole in the ground, the Pathfinding would invalidate cells where you can throw a grenade at (depending of course on your game rules)
Plus a raycast should cost WAY less than checking if a cell is accessible from Pathfinfing

Hi @juergenarno! Welcome to the community

I implemented this using a raycast. Here is my code with comments

    private bool ObstacleInTheWay(GridPosition unitGridPosition, GridPosition targetGridPosition)
    {
        // Get the unit world position
        var unitWorldPosition = LevelGrid.Instance.GetWorldPosition(unitGridPosition);
        // Get the target world position
        var targetWorldPosition = LevelGrid.Instance.GetWorldPosition(targetGridPosition);
        // Get the direction to the target
        var directionToTarget = (targetWorldPosition - unitWorldPosition).normalized;
        // Get the distance to the target
        var distanceToTarget = Vector3.Distance(unitWorldPosition, targetWorldPosition);
        // Create the ray - with height offset
        var offset = Vector3.up * 1f; // <- magic number. sorry
        var ray = new Ray(unitWorldPosition + offset, directionToTarget);
        // Check if there is an obstacle in the way and return
        return Physics.Raycast(ray, distanceToTarget, _obstacleLayerMask);
    }

It gets called in GetValidActionGridPositions just before we add it to the list

    if (ObstacleInTheWay(unitGridPosition, testGridPosition)) continue;

    validGridPositions.Add(testGridPosition);
5 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms