Jump to content

AI attack range


Slimwaffle
 Share

Recommended Posts

If the minimap you are referring to was the simple shader and script version I posted and then Aggror added a sphere to it to check for nearby enemies, then the problem could be that the sphere is being set as a child to the player. Twice for some reason? If i had to guess  what the problem was without testing it out, i would say its because the sphere is now a child of the player which the enemy AI script assumes the sphere is the player. The sphere is 50 meters in diameter.

function Script:Start()
    self.miniMapSphere = Model:Sphere(8,self.player) -- by putting the player as the second parameter, you just set the sphere as a child to the player
    self.miniMapSphere:SetScale(50,50,50)
    self.miniMapSphere:SetPosition(self.player:GetPosition(true))
    
    --Parenting the sphere to the player, means we dont have to update the position ourselves again.
    self.miniMapSphere:SetParent(self.player) -- this is duplicating setting the parent?
    self.miniMapSphere:Hide()
end

so what you can try is to remove the parenting and then update the position of the sphere in the Script:UpdateWorld() function.

function Script:Start()
    self.miniMapSphere = Model:Sphere(8)
    self.miniMapSphere:SetScale(50,50,50)
    self.miniMapSphere:SetPosition(self.player:GetPosition(true))
    self.miniMapSphere:Hide()
end
function Script:UpdateWorld()
    self.timer = self.timer + Time:GetSpeed()
	self.miniMapSphere:SetPosition(self.player:GetPosition(true)) --add this to update the position of the sphere
    if self.timer > self.timeUntilMinimapUpdates then
        self.timer = 0
        self.enemyEntities = nil
        local AABB = self.miniMapSphere:GetAABB(1)

        --We pass along the entity that our current script is attached to
        world:ForEachEntityInAABBDo(AABB, "Callback", self.entity) 
    end
end

If the above is not the latest and greatest code you are working from, then my apologies. This is just a guess since i don't have an easy way to test it out, but it may be worth a try to see if it resolves your issue.

 

--EDIT: I just tested my theory above by placing a crawler with the standard monster AI script into a simple terrain scene in the editor. I changed the visual range to 5 meters. i then placed a player prefab in the scene about 30 meters away. Then I created a 50-m diameter CSG sphere with the invisible material. Placed the sphere's center at the location of the player and dragged the sphere in the scene browser to be a child under the player. Started the game and once the player moves a couple of steps toward the crawler, and it immediately starts to chase me. So it looks like my theory may be correct.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Again, I can't speak for what you have because I do not have it. If you are using the standard monster AI script on the crawler, then it should work correctly just like the test that I did above. If you are using your own custom scripts for the AI and whatever you currently have for the minimap, then nobody but you probably has that exact setup. Provide an example project that shows the issue and someone may be willing to help troubleshoot. 

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Ok so I added this line in the HUD AI under ChooseTarget():


 if d < self.sightradius then


..so it looks like this:

function Script:ChooseTarget()
    local entities = GetEntityNeighbors(self.entity,self.sightradius,true)
    local k,entity
    for k,entity in pairs(entities) do
        if entity.script.teamid~=nil and entity.script.teamid~=0 and entity.script.teamid~=self.teamid then
            if entity.script.health>0 then
                local d = self.entity:GetDistance(entity)
                local pickinfo=PickInfo()
                if self.entity.world:Pick(self.entity:GetPosition()+Vec3(0,1.6,0),entity:GetPosition()+Vec3(0,1.6,0),pickinfo,0,false,Collision.LineOfSight)==false then
                    if d < self.sightradius then --added so they don't charge from any distance
                    return entity.script
                    end
                end
            end
        end
    end
end

 

Not sure why that's not in the original script. So to OP just what Macklebee suggested above, plus change this one line in your HUD AI script, save it as a prefab if you're spawning them, and also I have to change this line in the minimap script from a 1 to a 2 for the dots to appear:

local AABB = self.miniMapSphere:GetAABB(1)

...so it looks like:
local AABB = self.miniMapSphere:GetAABB(2)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...