Dec
9
2012

Enemy Visibility Cone (idea)

Testing one visibility cone idea for enemies. (later will check what other solutions there are..)

Code:

  1. If enemy stays inside blue trigger area (attached to player) then
    1. If enemy view angle is towards the player (within view angle range) then
      1. Do Linecast(s) to player bounding circle/sphere then
        1. if LineCast doesn’t hit obstacles, we can see the player

Now just need to try that in Unity..

Version 1.0 notes
Vector3.Dot used to check if player is front of the enemy (theres ready code in docs)
Physics.LineCast for checking if there are any walls between player-enemy
Quaternion.LookRotation & Quaternion.Slerp for rotating & turning towards the player
Vector3.MoveTowards for moving towards player

Webplayer:
http://unitycoder.com/upload/demos/EnemyVisibilityCone1/ (v1.0)

Download source:
enemyVision1.unityPackage


11 Comments + Add Comment

  • Nice! Maybe you can enable the cone view only when the enemy is inside the blue area.

    Please post your results!

  • Added webplayer 1.0: Enemies rotate & move slowly towards the player, when player is visible in front of them. (doing just 1 linecast right now for checking if any walls are in between enemy to player)

  • I am working on something very similar to this in a project I am working on, always interesting to see someones take on it.

    I have the enemies once alert move towards the player, if the linecast is broken, the enemy will move towards the last place it saw the player without a broken linecast. once there it will start a random searching path for the player.

    my next step was to add in the outside linecasts so the enemy will have move information about the player to script AI off.

    I will keep an eye on what you are doing.

    • Had similar thoughts,

      – LastSeen spot is known, so enemy can move there
      – If enemy can see the player, he could alert the nearby enemies to turn that way (for example enable triggerbox around the enemy or Physics.OverlapSphere / SendMessage..)
      – Maybe could try some prediction (if the player runs towards north and, if the enemies get alerted far away also, they could have a temporary target on playerpos+20m north..), so they would be waiting him there.

  • i fell down the platform :'(

    reminds me of the plain code i use for my ships, i don’t reduce enemy sight to a cone yet, its 360°, maybe i should add it, would be more interesting if you can sneak up on ships (how would that even work in reality). I used coroutines to minimize the load when i have +60 ships flying around, they only get triggered every 0.5-2s depending if its raycastchecks or movement that has to be done.

  • It’l be good if it’s possible to do that for every vertex of player character.

    • Yeah..that could work especially on 3D (and if there is no hundreds of vertices), then enemy could see your leg under some object etc.

  • This was really nice and I can see it used in several of projects. Are you planing to share the code?

    Joakim

  • Webplayer source added.

  • Great idea!
    I love the phrasing, I can’t image that rea game devs do it any other way.

    Mars

  • I love you, I’ve been searching a way to do that with a Camera and field of view, but it was so easy. I put all these together and they work perfect. Thank you !

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.