• Nextbot stuck on walls and other objects
    2 replies, posted
This video shows my problem [url]https://www.youtube.com/watch?v=MSywkPO1XoQ&feature=youtu.be[/url] Here is the code for movement toward enemy [CODE]//ENEMY function ENT:ChaseEnemy( pos, options ) local enemy = self:GetEnemy() local options = options or {} local path = Path( "Follow" ) path:SetMinLookAheadDistance( options.lookahead or 300 ) path:SetGoalTolerance( options.tolerance or 20 ) path:Compute( self, pos ) if !enemy:IsValid() then return end if enemy:Health() < 0 then return end path:Compute( self, pos ) if ( !path:IsValid() ) then return "failed" end while ( path:IsValid() ) do path:Update( self ) path:Draw() if enemy and enemy:IsValid() and enemy:Health() > 0 then if !self.IsAttacking then if self:GetRangeTo( enemy ) < 600 then if math.random( 1,500 ) == 5 then self:IdleSounds() end end end end if ( self.loco:IsStuck() ) then self:HandleStuck() return "stuck" end if self:GetVelocity():Length() <= 0 then timer.Simple( 2, function() path:Compute( self, pos ) end) end if enemy and enemy:IsValid() and enemy:Health() > 0 then if self:GetRangeTo( enemy ) < 25 or self:AttackObject() then elseif self:GetRangeTo( enemy ) < 25 or self:AttackDoor() then end end if ( options.maxage ) then if ( path:GetAge() > options.maxage ) then return "timeout" end end if ( options.repath ) then if ( path:GetAge() > options.repath ) then path:Compute( self, pos ) end end coroutine.yield() end return "ok" end function ENT:GetEnemy() return self.Enemy end function ENT:SearchForEnemy( ents ) for k,v in pairs( ents ) do local enemy = math.random(1,2) if enemy == 1 then if v:IsPlayer() and v:Alive() then self:SetEnemy( v ) return true end end end self:SetEnemy( nil ) return false end function ENT:FindEnemy() self:SearchForEnemy( ents.FindByClass( "player_default" ) ) local players = player.GetAll() local entities = {} if players != nil then table.Add( entities, players ) end self:SearchForEnemy( entities ) end function ENT:HaveEnemy() local enemy = self:GetEnemy() if ( enemy and IsValid( enemy ) ) then if ( enemy:IsPlayer() and !enemy:Alive() ) then return self:FindEnemy() elseif ( enemy:IsNPC() and enemy:Health() < 0 ) then return self:FindEnemy() end return true else return self:FindEnemy() end end function ENT:SetEnemy( ent ) self.Enemy = ent if ent != nil then if !ent:IsValid() then return end if ent:Health() < 0 then return end end end[/CODE] Here is the code for movement [CODE]//MOVEMENT function ENT:RunBehaviour() while ( true ) do if self:HaveEnemy() then local enemy = self:GetEnemy() pos = enemy:GetPos() if ( pos ) then if enemy:Health() > 0 and enemy:IsValid() then self.HasNoEnemy = false if self:CheckStatus() then self:MovementFunctions( self.MoveType, self.WalkAnim, self.Speed, self.WalkSpeedAnimation ) end if ( self.loco:IsStuck() ) then self:HandleStuck() return "stuck" end local opts = { lookahead = 300, tolerance = 20, draw = false, maxage = 1, repath = 1 } self:ChaseEnemy( pos, opts ) end end else self.HasNoEnemy = true self:IdleFunction() end coroutine.yield() end end [/CODE] Does anyone have any idea how to fix this? Its really annoying and I can't find out what is wrong. If I did not provide enough code then I will send the rest its just really large.. The zombie navigates fine but it occasionally gets stuck.
There's a few things you can try. Basically you'll need to do additional pathing code to get a decent path around corners. [URL]http://wiki.garrysmod.com/page/Category:CNavArea[/URL] In particular, i used [URL]http://wiki.garrysmod.com/page/CNavArea/GetCenter[/URL] along with a few other bits and pieces to create prediction points that curve around the goal path so it's closer to the center of the nav area it's passing through, but not directly in the center. Should prevent the bots walking into walls like that.
Thank you! This helps a ton! :)
Sorry, you need to Log In to post a reply to this thread.