Wednesday, June 18, 2014

Unity, SphereCast VS MeshCollider Performance test results





There are 5 Test Objects against 100Boxes with Colliders












Each one will do either SphereCasts or AutoGenerate MeshCollider everyFrame.


Test1
Each Test Object do SphereCast once per frame,













 


Result, Extra Calculation only got made when SphereCast Hit Objects.

Test2
Each Test Object do GenerateMeshCollider Every Frame
**During Actual GamePlay, will only be on during Attack**
























Result, will do Calculation regardless of whether Object got hit or not.



Test3 do SphereCast 10 times each Frame




 


 Test4 Generate MeshCollider 10 times each Frame

 


 








 From an Extra test, Generated Mesh is independent of Render Mesh.





Overall Results

For SphereCast, significance performance Cost will occur only when Casting hit any Collider.

For MeshCollider, Performance is constant regardless of whether Collision occur or not.

Advantages

SphereCast - No need to setup RigidBody and Collider on Weapon Object.

MeshCollier - Can make precise Hit Detection even when done only once per frame

Disadvantages



SphereCast - Need to do multipleCast, depending on circumstances.


MeshCollier - Can make precise Hit Detection even only once per frame
Hypothesis

During Actual Gameplay,
 
Even though MeshCollider have performance issue even when not hitting anything,
it will get call only when MeleeAttack  is On.

Also, Under some Circumstances, SphereCast need to be calculated multiple times to get good coverage.
While, MeshCollider only need to be call once to perform a perfect coverage.

Performance-wise, MeshCollider is an overall-winner.

For the Setup,
As long as WeaponPosition can be obtain, Vertices Positions can be calculated from Character Transform.
However, because base GameObject need its own Collider and RigidBody for other purpose,
Components for Hit Detection need to be put in Other Object.
And because OnTrigger() can only be call on the Object with Collider.
This also result in the need to do data referencing across multiple Objects.

Structure-wise, SphereCast is better to setup.

Conclusion

Generate MeshCollider is a Winner for this purpose.
However, it would be better if there is a way to see whether a referenced object make any collision or not.











No comments:

Post a Comment