Search Unity

Is Ray Raycast optimize for AI path finding

Discussion in 'Physics' started by michealcaj, Oct 4, 2021.

  1. michealcaj

    michealcaj

    Joined:
    Aug 18, 2017
    Posts:
    191
    Hello lads ,

    So I'm working on a working on a traffic simulator ND I want to shoot 5 Raycast each for every AI vehicle spawned , this would serve as ai sensor,

    Would this affect the performance
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
  3. michealcaj

    michealcaj

    Joined:
    Aug 18, 2017
    Posts:
    191
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    i'd test raycast and check profiler how much it costs..

    optionally,
    do you need to raycast every frame, or it could be delayed to a slower rate?
     
  5. michealcaj

    michealcaj

    Joined:
    Aug 18, 2017
    Posts:
    191
    No it doesn't always need to be there always, only when ai get close to other vehicles or player, any idea how I can do that
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    could use triggers (like each vehicle has invisible sphere collider trigger around them),
    physics.overlapsphere from script, Coroutine that only runs every x seconds,
    Space partitioning (basically only check distances to objects that are nearby within same or neighbor cell), Also can assign suitable physics layers and use layermasks (to only check raycast hits with certain layer).

    Or, for each vehicle, could cast forward ray every frame, but other rays only every x frames.
    Or, instead of raycasts, attach actual meshes with trigger colliders as "rays" on the vehicles.

    but still, i'd try brute force all rays first, and see if that can be made fast enough already.
     
    michealcaj likes this.
  7. michealcaj

    michealcaj

    Joined:
    Aug 18, 2017
    Posts:
    191
    Lovely thansks mate