Jump to content

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


Haydenmango
 Share

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

Link to comment
Share on other sites

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?

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

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.

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

  • Upvote 1
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...