Change crosshair when the enemy is in the players view?

Hi!

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)


and here where the enemy is out of the cameras view (crosshair normal)


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…

one moment I will dig

I am having trouble finding the code… monster?

I think this is the right direction.

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.

can you use radar?

has performance for radar Vs collision Vs Ray pattern ever been tested?

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.

Thanks for all the replies, i’d appreciate a .blend file if anyone figures it out.
Once again, thanks

Send a message to the crosshair object. Change the mesh used. When the mouse is over an enemy (sensor) send a message (actuator) to your MouseObject.

So i set this up with an always sensor? cause i can’t get it to work in 2.49. :confused:

Test with default cube: Sensor(Always)—>Controller(Python-With the script)—>Actuator(Motion-Rotate)

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! :confused:

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.

This is just what I need! I can’t thank you enough! :slight_smile:


Hey! Can you help me out here, pls? :s