Search Unity

Unity Physics: Colliders or Raycast?

Discussion in 'Physics' started by viknesh2020, Feb 18, 2019.

  1. viknesh2020

    viknesh2020

    Joined:
    Jul 19, 2016
    Posts:
    52
    Hi everyone !

    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.

    -Cheers !
     
  2. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    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.
     
    viknesh2020 likes this.
  3. viknesh2020

    viknesh2020

    Joined:
    Jul 19, 2016
    Posts:
    52
    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?
     
  4. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    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:

    Code (CSharp):
    1.  
    2.  
    3. //Enemy script
    4.  
    5. [SerializeField] Transform target;   // the player
    6. [SerializeField] float distLevel1;
    7. [SerializeField] float distLevel2;
    8. [SerializeField] float distLevel3;
    9.  
    10. // with distLevel1 > distLevel2 > distLevel3
    11. ...
    12.  
    13. float distance = Vector3.Distance( transform.position , target.position );
    14.  
    15. if( distance < distLevel3 )
    16. {
    17.     DoAggressionLevel3():
    18. }else if( distance < distLevel2 )
    19. {
    20.     DoAggressionLevel2():
    21. } else if( distance < distLevel1 )
    22. {
    23.     DoAggressionLevel1():
    24. } // maybe an additional "else" , a "default" case


    Regards.
     
    OnovicRichard likes this.
  5. viknesh2020

    viknesh2020

    Joined:
    Jul 19, 2016
    Posts:
    52
    Thank you very much ! Sorry for my late reply.

    I completely forgot Vector3.Distance. This looks much more simple than ray/sphere cast and colliders.

    I will definitely use this !

    You deserve a badge :)
     
    lightbug14 likes this.
  6. Michael_Swan

    Michael_Swan

    Joined:
    Aug 24, 2021
    Posts:
    59
    Also consider using sqrmagnitude and comparing squared distances, as it's faster.
     
    viknesh2020 likes this.