Grenade can be thrown through wall

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