- Home /
Distance Calculation vs OnTriggerStay
Greetings,
I'd like to know, which would use less CPU clock.
Briefly instantiate a Trigger sphere collision with a function OnTriggerStay.
Or
Calculate the distance between two objects and initialize the proper functions accordingly?
Thank You
Answer by dannyskim · Jan 10, 2012 at 05:26 PM
Basic math calculations are always going to be cheaper than using the OnTrigger or OnCollision method groups because of built in overhead when utilizing them. Assuming you cache your variables correctly in your own distance code, it should be faster, but I don't know if it will be an appreciable difference if profiled unless you're making heavy use of OnTrigger.
It's to be used within the AI of an enemies, since the game is isometric I don't need height calculation and so I can easily have the wanted "Circle of Effect" by calculating distance between Enemy and Player.
It will be calculated each times the player homes-in to kill the enemy.
If your target is desktop platforms, you may still want to consider using OnTriggerStay. The Collider information passed from OnTrigger might prove to be useful, and it might be a welcome tradeoff compared to the $$anonymous$$or performance differences by calculating it yourself. It really all depends on what kind of AI and effects you plan to incorporate.
There's no platfor$$anonymous$$g, it's more Hack'n'Slash. The OnTrigger would only serve to trigger a Send$$anonymous$$essage and an animation. It wouldn't do anything else. Also, the only object susceptible to trigger this should be the player.
So since it's so predictable, I would rather go for the performance route. As to insure I can run the game to the lowest performance possible.
By platform, i meant if for $$anonymous$$obile, Web, PC, $$anonymous$$ac, etc. I agree you should use a basic distance calculation for that use case though.
Answer by arcghost · Jan 10, 2012 at 07:35 PM
A spherical trigger uses the Distance-calculation and some additional comparisons(object name, is it a trigger, and things like that).
A sphere has a radius and that is the distance from the center to the edge.
I made an example for you: So calculating the distance costs less cpu power, because there are less things to check.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062839im_/https://answers.unity.com/themes/thub/images/avi.jpg)