playMaker

Author Topic: [solved] What is more efficient - Wait or (Get Distance and Float Compare)?  (Read 2301 times)

createasaurus

  • Full Member
  • ***
  • Posts: 146
Hi,

I'd like my enemies to be active when the player is near - but OFF when player is far.  Currently I have Get Distance and Float Compare running every frame for each enemy.  When player is near this triggers the complex enemy behaviors.

Question: is it actually expensive to be running multiple Get Distance and Float Compare every frame?  Should I go in and put a Wait on each enemy, like for every second, so the Player is not constantly measured?

Or is "Wait" just running every frame anyway, and Get Distance and Float Compare not expensive after all - so there would not be much improvement?

Thoughts?
Thank you!!
« Last Edit: February 24, 2016, 09:07:20 AM by createasaurus »

Vallar

  • Junior Playmaker
  • **
  • Posts: 90
I don't know how taxing this is from the solution I am proposing but I believe another way to do this is to put a child object to your enemy. Have it an empty child with only a collider acting as the area (essentially the distance you are referring to) you would like to measure.

Then you'd have an action of "On Trigger Event" that fires off when the player enters the trigger.

4ppleseed

  • Full Member
  • ***
  • Posts: 226
Hi,

I'd like my enemies to be active when the player is near - but OFF when player is far.  Currently I have Get Distance and Float Compare running every frame for each enemy.  When player is near this triggers the complex enemy behaviors.

Question: is it actually expensive to be running multiple Get Distance and Float Compare every frame?  Should I go in and put a Wait on each enemy, like for every second, so the Player is not constantly measured?

Or is "Wait" just running every frame anyway, and Get Distance and Float Compare not expensive after all - so there would not be much improvement?

Thoughts?
Thank you!!

A 1 second Wait has to be better than a Get Distance per frame.

I'm pretty sure Unity sets a flag for 1 second and comes back to the wait command then moves on. I can kinda see what you're thinking about it waiting every frame for up to 1 second but if all the code did that it wouldn't be optimised at all.

I have an enemy that checks every 1 second if the player is less than a certain distance away and it felt a little slow. I brought it down to half a second (30 frames) and it felt much better and responsive. So I say, start at 1 second and see how it feels.

createasaurus

  • Full Member
  • ***
  • Posts: 146
Code: [Select]
I'm pretty sure Unity sets a flag for 1 second and comes back to the wait command then moves on. Thank you!!  This is exactly the info I was hoping for.