Can anyone tell me which one is more is more efficient(If either is more than the other)
For these types of questions there is very often not a clear answer but it’s also very easy to test this yourself by creating a basic stress test of each and looking at the profiler results. Even better, you can test it in a more realistic environment in your project which gives you results for your specific use-case.
I would only add that this kind of question is only important if you intend on performing a lot of these queries per-frame because individually they are relatively inexpensive.
For me, SphereCastNonAlloc was 1.5x faster than the capsule one.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TheCOLDDev.Utilities;
public class TT_OverlapStressTest : MonoBehaviour
{
public Collider[] m_Col;
public float radius;
public float height;
public bool Capsule = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
m_Col = new Collider[32];
if(!Capsule)Physics.OverlapSphereNonAlloc(transform.position , radius , m_Col , TC_Utilities.EverythingLayer() , QueryTriggerInteraction.Collide);
else Physics.OverlapCapsuleNonAlloc(transform.position + transform.up * height/2 , transform.position - transform.up * 0.5f *height, radius , m_Col ,TC_Utilities.EverythingLayer() , QueryTriggerInteraction.Collide);
}
}
with around 250 Calls per second(different instances) for 100 colliders
I would expect Sphere/Circle to be the fastest because it’s a simpler collision test overall, certainly sphere/sphere and circle/circle but certainly the profiler is the real measure as the pure collision detection is only part of it.
You might be better off performing them both in a loop many times per-tick to get more accurate results but your results sound valid.