#ClosestTransform

1 messages Ā· Page 1 of 1 (latest)

weary dirge
#

Ye?

hasty lake
#

Im thinking of a solution, I need some time

#

all right I think I got it working

weary dirge
hasty lake
#

but that'll surely run much much better

#

thank you for your help!

#

Yeah it seems to work

#

after the AI changes

weary dirge
# hasty lake thank you for your help!

Np šŸ‘ I’m going bed now so I’m not able to answer in few hours. You can ask on main channel if you need some help. Feel free to ping me if you have some updates about this

hasty lake
#

well I'll go to bed aswell, it's 00:31 here

weary dirge
#

It’s 1:31 here šŸ˜…

hasty lake
#

even better

weary dirge
#

@hasty lake actually I just realized if you do the closest search like you do in here https://discordapp.com/channels/489222168727519232/763495187787677697/966800229867335680, enemies > 0 can give true even if there's no objects satisfying the condition GetComponent<UnitAI>().agentColor != agentColor (because the overlapsphere can find only objects that are not satisfying the agentColor condition. let's say overlapsphere finds only 1 object but for that object GetComponent<UnitAI>().agentColor != agentColor is false so enemies will be 1 even if no acceptable object was found. I think I'm now explaining this way too complicatedly). Checking for tMin == null (in the part of code where you decide whether you found any enemy AIs) instead should work tho

hasty lake
hasty lake
#

Well I found a little problem

#

(I've temporarily set their radius to something small) many of the troops won't see other enemies

#

even though looking for them runs every frameof these who don't have a target

#

im not looking for the closest one atm. That's the current functio

   public bool LookForEnemies()
    {
        for (int i = 0; i < Physics.OverlapSphereNonAlloc(transform.position, unitAttributes.detectionRange, unitBuffer, targetLayerMask, QueryTriggerInteraction.Ignore); i++)
        {
            if (unitBuffer[i].GetComponent<UnitAI>().agentColor != agentColor)
            {
                agentGlobalTarget = unitBuffer[i].transform;
                return true;
            }
        }
        return false;
    }
#

For some reason, it's difficult for them to detect enemies when they are moving

#

even though that's the whole code handling it

if (agentState == AgentState.move)
        {
            if (LookForEnemies()) agentState = AgentState.attack;
            else agentState = AgentState.move;
        }
#

It's supposed to be like that so they are not looking for new enemies while in attack mode

hasty lake
hasty lake
hasty lake
#

All right, a breakthrough! using OverlapSphere, allocating a new array and using foreach solved the issue. I think it's still running well

#

buffer could have been not big enough

#

or it was some deeper issue

weary dirge
# hasty lake im not looking for the closest one atm. That's the current functio ```cs publ...

I don’t know if this is the issue here but don’t put anything expensive in the for loop condition. The condition will be evaluated every iteration so it can be very expensive to put OverlapSphereNonAlloc in the condition as it will run every iteration (https://stackoverflow.com/questions/655373/is-the-condition-in-a-for-loop-evaluated-each-iteration). You could just precalculate the amount of objects the line above like int someVariableName = Physics.OverlapSphere…. I’m not sure if that can cause any problems other than performance impact (if the order of elements OverlapSphere gives can change, that would be a problem but I don’t know how that works so can’t really tell if that caused the issue you was having)