Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

Question

i have it so the navmesh "Controller" makes the game-object go to the player if they are in range(The player is selected by closest player, can change on update if another player is closer). then if the enemy/gameobject get close to the player the Controller will stop the navmesh, and the gameobject/enemy will start playing the shooting animation. but i have one main issue. The enemy stops even if he can not see you(Like if you are in a building) should i just have two types of enemies, being one is for inside only and the other is for outside only? or is there a way to check if view is obstructed?

example of obstructed view:

Please login or register to see this link.

Example of normal:

Please login or register to see this link.

 

My current code:

Please login or register to see this code.

Share this post


Link to post
Share on other sites

13 answers to this question

Recommended Posts


  • 0

maybe draw raycast from the viewpoint of the enemy to the player, and if it hits something it means he cant see the player. Although this is pretty inaccurate as it focusses on 1 point of the player, meaning if the enemy should actually see a part of the player, but the raycast doesn't, the enemy wont take any action. So maybe multiple raycasts is an option, or you should google for some tutorials.

Share this post


Link to post
Share on other sites
  • 0

You could do something like this

Please login or register to see this code.

And another thing i like to do for AI is store it current states so its easier in the end set it chase, attack etc

Please login or register to see this code. Edited by nub
  • Like 1

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

i could just add a player tall trigger as long as the stop distance. and check colliding items that have the tag of barrier or building, if they do then i can enable the navscript.

Share this post


Link to post
Share on other sites
  • 0

You should a vector that goes from AI position to player position, then use the InverseTransform function for the AI's local forward vector to make it a global vector. Now calculate the engle between both vectors using Vector3.Angle, and if the angle is greater than n degrees... The AI is looking at the player. To check if the player isn't hidden behind an object, cast a ray from the AI to the player using Physics.Raycast, and if the output RaycastHit contains a game object with the tag "Player" (for example... there are other ways of detecting player hits), than Voila, the AI is looking at the player.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

so i have this:

Please login or register to see this code.

it is in the update section, so shouldn't it always be looking at the player/and have a view of the torso of the player?

because it doesn't really work, i can post a video showing the whole script and what happens in-game. btw idk but it was actually moderately working earlier. idk what i did, i changed nothing effecting this, lol.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote. Please login or register to see this code.

you are raycasting only one line so when player is hit on that one line and distance is right your AI will do something...

 

curDistance gets updated?

sure player hits the line?

 

 

figure out where code is failing just by adding print("test 1") etc in functions

Edited by nub

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

if the player is not hitting the line, it should reactivate the navmesh untill it does hit the player correct? btw i did a bit of testing and it only showed up in consol once.

Edited by KrypTik_Sweller

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

oh yes didnt notice but you got this wrong

Please login or register to see this code.

should be inside if( )

Please login or register to see this code.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

far as i have understood it is, well it returns bool "public static bool" so you can if it or save it some bool test or something

Please login or register to see this link.

 

i copied it from my earlier post

 

and

Please login or register to see this code.

if this showed up in console after player was front of the AI it means your code works so far

Edited by nub

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

well, it did once, it is supposed to turn(enable navmesh) every time it cant see the player, then it would print it again. but it doesn't. it acts like the raycast only shoots once, or something.

Share this post


Link to post
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.

Guest
Answer this question...

×   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...