How to declare that object have been hitted by 2 or more raycast?

6406354--715093--Honeycam 2020-10-11 18-27-27.gif

Title is description.

You can add all the relevant colliders you hit to a list.

if(hit)
{
hits.Add(hit.collider);
}

Then you group them.

// using System.Linq;
var groupedHits = hits.GroupBy( i => i );

And then you can check for each collider, which is the h.Key, and how many occurences of it in the groupedHits list with h.Count();

{
if(h.Count() > 2)
{
Debug.Log(h.Key + " was hit " + h.Count() + " times by the Raycasts");
}
}```

Don't think this is the most optimized solution to run every frame tho.

Now, when I want to do something like you're doing, that is, detecting objects within a FOV. I usually do a OverlapSphere with a mask for what I'm looking for, and test to see if they're inside the FOV after detecting them. If I want to detect if there's a wall in between, I do a single raycast in their direction and see if it hits a wall or not, you might have other plans and this solution might not work, just thought it might help.
2 Likes

yeah, I tried using single raycast to checking hitting the wall
however It wont work so well, it sometime detect or not depending on position

https://forum.unity.com/threads/why-my-enemys-fov-kinda-buggy.984184/

I think this problem conflict by tilemap collider or my script caused the logic error
anyway, thank you for comment.

[edit]
It was the logic error.
by following the // https://github.com/SebLague/Field-of-View //
the error was I didn’t calculate the distance of target but collider
I dont know What Im talking right now but well, whatever.

1 Like

So everything’s working now?

yeah, thanks.