# playMaker

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

#### createasaurus

• Full Member
• Posts: 145
##### [solved] What is more efficient - Wait or (Get Distance and Float Compare)?
« on: February 12, 2016, 06:18:13 AM »
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, 07:07:20 AM by createasaurus »

#### Vallar

• Junior Playmaker
• Posts: 90
##### Re: What is more efficient - Wait or (Get Distance and Float Compare every frame)?
« Reply #1 on: February 12, 2016, 12:26:33 PM »
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: 227
##### Re: What is more efficient - Wait or (Get Distance and Float Compare every frame)?
« Reply #2 on: February 15, 2016, 08:40:20 AM »
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: 145
##### Re: What is more efficient - Wait or (Get Distance and Float Compare every frame)?
« Reply #3 on: February 24, 2016, 07:06:31 AM »
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.