Jump to content
Haydenmango

Adding 'Line of Sight' to Computer Enemies(Lua)

Recommended Posts

Hello leadwerks community! I have been trying to find a good way to add Line of Sight to ranged enemies.

 

At the moment I can get them to attack from a decent range by taking the MonsterAI.lua script from the tutorials and changing the attack range to a higher number.

 

Now I run into the issue of the enemies being able to attack me when I am behind objects like walls/trees/etc. So I have attempted using the Pick() function but I just can't seem to figure it out.

 

Can anybody out there point me in the right direction or give me some ideas for how to do this?

Any help would be much appreciated.

Share this post


Link to post

Using the Pick() function is what you want. What issues are you running into when using it? You should be picking from the enemies position to the players position and check the colliding entity to see if it's the player or not.Be sure the pick locations are a little higher than the y position of the entities (otherwise it's right on the ground and it might hit the ground first). Also be sure to hide the enemy calling the pick as it could pick it'self (because the position location is inside the model itself). You can hide it, pick, show it.

Share this post


Link to post

Hi Rick! Thanks for the quick response. Well I made the a sphere that moves to the pick location and it always appears right on the picking entity.

 

I think Hiding the Picking Entity and raising the Pick Height should solve the problem because it seems the picking entity (or the ground right in front of the picking entity) is blocking the pick from reaching its target currently. I am going to go see if this works in a little while, thanks again!

Share this post


Link to post

Using the Pick() function is what you want. What issues are you running into when using it? You should be picking from the enemies position to the players position and check the colliding entity to see if it's the player or not.Be sure the pick locations are a little higher than the y position of the entities (otherwise it's right on the ground and it might hit the ground first). Also be sure to hide the enemy calling the pick as it could pick it'self (because the position location is inside the model itself). You can hide it, pick, show it.

 

well rick is it more efficient than looking if it's less than 45° on the forward ray?

Share this post


Link to post

Hey Rick,

 

Instead of hiding the model I believe the proper way is to use SetPickMode(0) before the pick and SetPickMode(Pick::Sphere) after.

Share this post


Link to post

well rick is it more efficient than looking if it's less than 45° on the forward ray?

What has this to do with LOS? LOS means "is there anything between me and my target" (to check for example if there's a wall between us), while yours is to check if it's in front of me that is completely different question.

 

Hey Rick,

 

Instead of hiding the model I believe the proper way is to use SetPickMode(0) before the pick and SetPickMode(Pick::Sphere) after.

Agree, I think this should be the way to go, though I'm not sure it will skip also its children (in which case you'd need to call it on every child that you know having a collider). Do you know if it skips children too?

Share this post


Link to post

What has this to do with LOS? LOS means "is there anything between me and my target" (to check for example if there's a wall between us), while yours is to check if it's in front of me that is completely different question.

 

not really LOS is first checking if it's in front of you, after that you just raycast IF ray from you to target is clean. so if it returnes target it means it can see if returns wall or nothing means it cannot see OR it's too far away to pick anything.

Share this post


Link to post

Thanks everyone for the advice!

So a list of things I had wrong that are now fixed with solutions.

1-Pick Height was to Low

solution=raise the Y of the starting and ending points of the Pick (thank you Rick)

2-Pick would stop at Picking Entity

solution=SetPickMode(0) Before picking then SetPickMode(1) when finished (thank you Tjheldna)

 

I still have an issue where the Pick will not recognize my player(the target) for whatever reason so until I fix that my code is similar to this-

if pickInfo.entity ~= nil then

move to player

elseif pickInfo.entity== nil then

attack player

end

 

If the pick recognized my player then I would do

 

if pickInfo.entity==player then

attack player

end

Share this post


Link to post

You can either set a variable in the script that is attached to the player and check for the existence of that script variable via the pickinfo.entity or you can set an entity key in the player script and look for that.

 

script way:

 

Inside FPSPlayer.lua Start add:

 

self.isPlayer = true

 

You pick code:

if pickinfo.entity.script.isPlayer ~= nil then
  if pickinfo.entity.script.isPlayer == true then
     attack player
  end
end

 

Note that maybe just the existence of isPlayer (which is what the first if statement with the nil is checking) is enough.

 

 

If you wanted to use entity key then:

 

Inside FPSPlayer.lua's Start()

 

self.entity:SetKeyValue("isplayer", "true")

 

 

Inside your pick:

 

if pickinfo.entity:GetKeyValue("isplayer", "false") == "true" then
  attack player
end

 

 

Note in my case above you'll always want to check pickinfo.entity against nil before doing the code I have.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...