RaycastAll not hitting

Hello all,

I have a script creating a custom mesh as FOV around my charakter in a specific distance. if colliding with something, the mash will be interrupted* and created up until this obstacle but not behind. In total this should behave like the FOV in “Desperados” but 360°.

Actually this works, i have added a mesh renderer to visualize the result and i can see a wonderfull purple circle arround my charakter and is updating and moving with my movement of the charakter.

in addition i added some code for the interruption as described but the raycast which should help me there is not colliding with any obstacle i add to the scene. raycast hit.collider is always NULL.

I looked arround in the internet and even my own code. The main error cases explained in the internet are that other programmers should as solution add a collider to the obstacles but this was already done back in my own code. I even played arround with layermasks and added different layer to the different obstacles with collider.

As in my picture you can see:

  • the circle (purple) successfully was created around my character and is updated in every frame
  • white cylinders arround the map (with colliders)

My expectations:

  • seen from my character behind the white obstacles carved the purple circle because the raycast in code example down marked with → //casting ray // casting ray end ←
  • after raycast casted → vertex = hits[0].point ← should return the point of the first collision, in my example the collision with the white obstacles (which all have colliders) , and creating the mesh only until the white obstacle, not further

What i tryed:

  • added the layermask property
  • tryed Raycast instead of RaycastAll
  • added different layers to all the obstacles, all obstacles in my picture have different layers
  • changed layer of player

i don’t know where im blind here. Unbelievable the same method works differnt in other places of my project.

private void Start()
    {
        mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;   // -> creating custom mesh
        origin = Vector3.zero + highFromOrigin;           // -> adding some high so it is at middle high of my character

        verticies = new Vector3[rayCount + 2];
        uv = new Vector2[verticies.Length];
        triangles = new int[rayCount * 3];

        verticies[0] = Vector3.zero;
        verticies[1] = new Vector3(50, 0);
        verticies[2] = new Vector3(0, -50);

        triangles[0] = 0;
        triangles[1] = 1;
        triangles[2] = 2;
    }


public void calcFOV(float viewDistance)
    {
        float angle = startInAngle;
        float angleIncrease = fov / rayCount;
       
        verticies[0] = origin;

        int vertexIndex = 1;
        int triangleIndex = 0;

        for (int i = 0; i <= rayCount; i++)
        {
            Vector3 direction = GetVectorFromAngle(angle); // -> making a vector out of an angle
            Vector3 vertex;

            // casting ray:
            RaycastHit[] hits = Physics.RaycastAll(origin, direction, viewDistance); 
            if (hits.Length == 0)   // -> always null
            {
                Debug.Log("lol"); // -> debug
                vertex = origin + direction * viewDistance;
            }
            else
            {               
                vertex = hits[0].point; // never comes here
            } // casting ray end

            verticies[vertexIndex] = vertex;           

            if(i > 0)
            {
                triangles[triangleIndex + 0] = 0;
                triangles[triangleIndex + 1] = vertexIndex - 1;
                triangles[triangleIndex + 2] = vertexIndex;

                triangleIndex += 3;
            }

            vertexIndex++;
            angle -= angleIncrease;
        }

        mesh.vertices = verticies;
        mesh.uv = uv;
        mesh.triangles = triangles;
    }

    public static Vector3 GetVectorFromAngle(float angle)
    {
        float angleRad = angle * (Mathf.PI / 180f);
        return new Vector3(Mathf.Cos(angleRad), 0, Mathf.Sin(angleRad));
    }

Found it today.

the solution was, the raycast works well, pretty well but the repositioning of my field was incorrect. It currently calculates the collision of another point, creates the mesh and transports it to the player. I found it out by larger the check distance by far and suddenly saw it colliding in a far distance.

Solved.

How can i close it?^^

1 Like

Closed for you.