#ClosestTransform
1 messages Ā· Page 1 of 1 (latest)
Nice. How is the performance now?
there are no lags, im still working with the AI to get it working correctly
but that'll surely run much much better
thank you for your help!
Yeah it seems to work
after the AI changes
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
well I'll go to bed aswell, it's 00:31 here
Itās 1:31 here š
even better
@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
I'll look into that later when I'll be at home, thanks for the interest though!
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
because in many cases they are finding enemies after reaching their destination and stopping, actually
so it's actually the opposite :v
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
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)