Search Unity

How efficient is Raycasting vs. Sweeptest?

Discussion in 'Scripting' started by Stephan-B, Jul 26, 2011.

  1. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    For detecting potential collisions or movement issues, I am curious to know which is more efficient in terms of performance. Physics.Raycast or any of variant (sphere, etc) or rigidbody.SweepTest?
     
    Last edited: Jul 26, 2011
  2. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    With any sort of testing it depends on the size of the test. for example doing a raycast over a populated scene with infinity distance is going to take more processing vs a sized/smaller distance. Raycast is always the fastest, but if you were to use sphere or capsule cast it shouldnt be because of performance but because of what you need to do. Like you shouldnt come to the approach of doing multiple raycasts to simulate a sphere cast, As long as you trust unity's implementation you should assume that any kind of shortcuts are done in the various overloads already.

    Sweep tests will do multiple raycasts or other shape casts to detect the first collision and it *should* be doing this as optimal as possible and probably better than most custom testing.

    So its with raycast being fastest you should still consider using SweepTest because of the possibility of SweepTest preparing the data once to be tested multiple times rather than doing multiple raycasts ~3 or more.

    But with that said a downside of sweeptest is you can't define custom layer masks like you would be able to with the other Physics. statics


    So i mean effeciency wise if you can use a raycast to get the job done, consider it first then sphere then capsule. But if your using it to actually test rigidbody collisions just use sweeptest
     
    Last edited: Jul 26, 2011
  3. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    If I understand correctly, the SweepTest uses raycasting in its implementation, so Raycasting is more efficient.

    Sphere or capsule cast are more expensive than single Raycast as sphere / capsule cast multiple rays to create the shape?

    Would casting 3 rays ... (left side of object, center, right side) to simulate a width but more or less costly than casting a sphere of that equivalent width? Same distance on rays or sphere... not to infinity

    Layer masks are great :)
     
  4. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    I would just go with sphere since its going to give you better / more accurate results. even if it comes out taking more time than 3 raycasts, you will end up the best hit data and will not have to worry about slim colliders fitting between the 3 points ( not to mention above and below )
     
  5. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Thank you :)