ItsSgtMarv
ItsSgtMarv

Reputation: 105

Physics.Raycast doesnt seem to work consistently

enter image description here

I'm working on a raycast based pathfinding system. Basically what I'm trying to do is generate points around an object/check if that object can reach those points, and check if those points can reach the target. The target is the green cylinder in the back of the photo. Here is my layer mask which basically says to ignore the player as a collider/obstacle:

layerMask = Physics.DefaultRaycastLayers & ~(1 << 3);

Here is my raycasting code:

    // Check if enemy can see player without any obstructions
    bool CanSeeDestination(Vector3 startingPoint, Vector3 destination)
    {
        if(Physics.Raycast(startingPoint, destination, 50f, layerMask))
        {
            Debug.DrawLine(startingPoint, destination, Color.red);
            return false;
        } else
        {
            Debug.DrawLine(startingPoint, destination, Color.green);
            return true;
        }
    }

And finally my pathfinding function:

    // Raycast based pathfinding
    void Pathfind()
    {
        List<Vector3> surroundingPoints = new List<Vector3>();

        bool foundTarget = false;

        // Nested loop to build surrounding points vector array
        for(var i = 1; i <= 10; i++)
        {
            for(var k = 1; k <= 10; k++)
            {
                // Offset by half of max to get negative distance
                int offsetI = i - 5;
                int offsetK = k - 5;

                surroundingPoints.Add(new Vector3(transform.localPosition.x + offsetI, stepOverHeight.y, transform.localPosition.z + offsetK));
            }
        }



        // Loop through array of surrounding vectors
        for(var m = 0; m < surroundingPoints.Count; m++)
        {
            // If enemy can reach this surrounding point and this surrounding point has an unobstructed path to the target
            if(CanSeeDestination(transform.localPosition, surroundingPoints[m]) && CanSeeDestination(surroundingPoints[m], player.transform.position))
            {
                float distanceFromEnemyToTarget = Vector3.Distance(transform.position, surroundingPoints[m]);
                float distanceFromTargetToPlayer = Vector3.Distance(surroundingPoints[m], player.transform.position);
                float totalDistance = distanceFromEnemyToTarget + distanceFromTargetToPlayer;

                // If this total path distance is shorter than current path distance set this as target
                if(totalDistance < currentPathDistance)
                {
                    currentPathDistance = totalDistance;
                    target = surroundingPoints[m];
                    foundTarget = true;
                }
            }
        }

        if (!foundTarget)
        {
            target = transform.position;
        }
        
    }

For some reason the raycasts trigger on the right side of the obstacle but not the left. Also if I increase the obstacle size or collider size I can eventually block the left side. Not sure why raycasts on the left are green and still passing through the collider.

Upvotes: 0

Views: 893

Answers (1)

ItsSgtMarv
ItsSgtMarv

Reputation: 105

I resolved the issue. The problem was in this line:

    if(Physics.Raycast(startingPoint, destination, 50f, layerMask))

I should have been using Physics.Linecast two go between two points. Raycast goes in a vector "Direction" linecast goes between two points. The correct code is:

    if(Physics.Linecast(startingPoint, destination, layerMask))

Upvotes: 1

Related Questions