I hope anyone can help me with this, i’m making an game (fps) and whenever the enemy is in the players sight it will change the crosshair to red.
I’ve tried to use ray for this but it only works when the enemy is right in middle of the screen (camera) and not when it’s in the players sight / view.
I guess this requires some coding, and since i’m an web-integrator so i know HTML, CSS, Javascript and PHP of course but no Python os i hope someone can help me?
i’m using 2.49
like here where the enemy is in the cameras view (crosshair red)
You can do it by making an invisible cone that represents the cameras field of view and then detect collision with enemies.
But I am sure someone will have a more elegant solution so wait for it.
Cast a ray? property enemy, Josip’s works,
in battlefield, Spot was a button, that basically cast a ray, and then your enemy has a “enemy” symbol follow them briefly in normal mode, and in hardcore, it only spots them on your map,
in multi-player this allows for one player to control the flow of combat, and earn points by doing recon,
this only works for the area under your crosshair,
there is some way to get a list of all the objects a camera is seeing though scene.something or other…
The KX_Camera object has a number of methods for testing if various shapes are inside the view frustum It can test a sphere, box, or point. So one way to do this is to simply iterate your enemies and test them all for being inside the frustum. I’m not sure how the performance of that would stack up against using an invisible object with a collision sensor for the frustum.
Optimizations would be quick testing of your enemies to see if you need to do a bounds check on them. For example, if enemies are behind the camera then don’t test their locations. It all comes down to how many enemies you have to check and how expensive that frustum check is relative to other tests.
from bge import logic
scene = logic.getCurrentScene()
py = logic.getCurrentController()
camera = scene.active_camera
ob = py.owner
if camera.pointInsideFrustum(ob.worldPosition) == 1:
for actuator in py.actuators:
py.activate(actuator)
This script will activate all actuators connected to it if the object with the script is in your view.
Oh I am sorry, its for 2.6. I will make a 2.49 version, give me a minute.
Do you need to trigger more than one actuator with it ?
One should be enough to send a message to the crosshair.
import GameLogic as logic
scene = logic.getCurrentScene()
py = logic.getCurrentController()
actuator = py.actuators[0]
camera = scene.active_camera
ob = py.owner
if camera.pointInsideFrustum(ob.worldPosition) == 1:
py.activate(actuator)
else:
py.deactivate(actuator)
Thank you so much! it works just fine, it triggers when ever the cube is in the view but i can’t turn it deactivate it again?
The crosshair just turns red when it receives the message. I’ve tried to invert it so when it’s not receiving the message it should replace the mesh again but i can’t get it to work!
import GameLogic as logic
scene = logic.getCurrentScene()
py = logic.getCurrentController()
camera = scene.active_camera
ob = py.owner
ob['InView'] = camera.pointInsideFrustum(ob.worldPosition)
This code will change the value of property called ‘InView’ you can then send or use that value.