AI Vision Ray?

So I was wondering, is it possible to add in like a cone, parent it to my enemy, and make it like a vision cone/ray, and so if I collide with it, I can still pass through it, but it sends a message to enemy to come and get me? I really need an answer to this question, because it is the last thing I have to implement.

You may ask someone(I think Smoking_mirror made that thing) to explain you how to detect player with raycasting. it should cast no-Xray rays from AI eye area to empties parented to the most important player bones. And each one who reaches the target should add +1 to visiblity. And, if visiblity > than some value you decide, than the AI switch state(easy method for begginers) and path-follows to player using navmesh. And if the visiblity gets down, he keeps following the player, but if he doesn’t get enaugh of visiblity for exact frame count, he returns to state 1(maybe something like this

if own["visible"] < 4: #in 4 there should be how much parts of the player he must see to track to player
    own["timer"] = 300 #frame count
    own["timer"] -= 1
if own["visible"] < 4 and own["timer"] < 1: #also make sure timer > 0 at beggining, for example- 20
    cont.activate(setState1) #actuator that sets to first state, call it like this: setState1 = cont.actuators("actuatorName")
    own["timer"] = 20

Just some basic ideas for your workflow, but not the best ones!

Hi! Radar sensor has a cone collision detection shape.

Radar sensor is X-Ray for our plague!

make a cone, set it to sensor, and you can do anything you want.
hit it, walk trough it, animate it, put a collision sensor on it and it do what you want

Here’s a simple python solution. First we ray cast to player to see if there’s a clear line of sight. Then we determine if the player is within the enemies visual field. It’s from a tutorial I’ve been working on this week

def spotPlayer(own):
    ob = own.rayCastTo(own['target'], 10, 'hidden')
    if ob and not ob['hidden']:
        coneVec = own.getAxisVect([0,10,0]) 
        vecTo = own.getVectTo(ob)[1] 
        angle = coneVec.angle(vecTo)
        if angle <= 0.5235: #angle of viewing cone in radians
            return False
    return False

own[‘target’] is the player (KX_GameObject) the enemy is looking for. ob[‘hidden’] is bool property held by the player to determine if the player is in hiding or not.

I’m gonna finish the tutorial tomorrow, with any luck, so there’ll be a full explanation coming.

import bge

cont = bge.logic.getCurrentController()

own = cont.owner()

Radar = cont.sensors['Radar']

if Radar.positive:
    Ray = own.rayCast(Radar.hitObject.worldPosition,own.worldPosition,0,'Unit',0,0,0)
    if Ray[0]:
        own['target']= Ray[0]
        own['DwellTime']= 180

if radar spots enemy, cast ray at enemy origin, if you dont hit the enemy with the ray, you don’t see them.

Hi blenderjunkie, I’ve actually made a video about 2 months ago that details how to make guards search for a player, and when they find the player they’ll follow you around. I use a radar sensor for this one, but it works just the same. :wink:

I’ll leave the video below if you need it, and I hope it helps. :slight_smile:

BGE - Adding Stealth With Guards (Python)

THank you all, but my game gave me the middle finger and started being apiece of crap so I won’t be entering

:frowning: Not nice to hear that :frowning: