I am working on a game where the enemy must detect the player and do some villainous stuffs. I am confused between using colliders or raycast/spherecast. All I need is a better performance low cost solution which is more adaptive irrespective of the 3d model which I am going to use.
Please pour in your experiences using Colliders and Raycasts and your thoughts on Which is efficient and better.
Hello, there is no “better”, it depends, do you have a lot of mesh colliders againt mesh colliders, that’s gonna be worst that having spheres vs spheres. Do you have 10 enemies or 1000 enemies?
And how is that you pretend to “detect” the player? has the enemy a certain range of sight in which can detect the target? (like the “Commandos” games), or is it enough with a “distance detection” (if distanceBetween enemy-player < X number → detected = true)? are there colliders that block that line of sight? all of this aspects matters.
The “ultimate” answer is of course doing the “performance test” yourself, but in my experience it depends of what exactly you want, for example, detecting the distance of a lot of enemies respect to the player (and getting the closer ones) with a simple distance detection algorithm can be worst that using an OnTriggerEnter, i assume this is because of how the physics engine works.
Thank you. Your response is exciting. I have planned the following,
There will be 10 enemies for first stage, 7 enemies for the second stage and 4 enemies for the third. I am creating a EnemyBrain.cs script for all of those enemies spawning in different positions over time. Each will have 3 raycast/spherecast, to show 3 different aggression towards player. Yes as you said they all distance bound. The more closer to the enemy the more the enemy aggression is. Player will not have any raycast, he will be running and shooting them with different types of guns and bombs. In the layermask property, I will only make the enemies to detect Player layer alone.
Player model came with meshrenderer component attached to it. I have added a Parent object to the player model, which have capsule collider and navmeshagent components. I am not sure about enemies whether they have mesh collider or not. The scene is filled with environmental props with various colliders.
You mean to say using three ray/spherecast is much costlier than using three different colliders with different radius each?
For the nav mesh agent alone it doesn’t need a collider, but for the physics stuff it does.
My bad, I meant that in a situation where the player has to detect the enmies nearby using a simple distance detection algorithm could be more expenssive that using a trigger, this is valid only if there are a lot of enemies, you will reach a crosspoint where performance is affected because you will have to get all the distance values from all the enemies availables (there is a workaround though).
Sorry but I still don’t get why you want to use a ray(sphere)cast … for distance detection you can go with the collider or with the distance detection algorithm, obviously in this case go with the distance detection algorithm. Something like this for example: