How do I make an armature objects head 'look' at something

Many games have what is known as ‘head tracking’, where a game character will look at an object like a chest on the ground for example. This can of course be done in Blender, but how would one go around implementing the head tracking in an armature, because then it would be visible both in Game Engine and viewport. Trouble is, it keeps screwing up the parenting whenever I try it. (head to body bone parenting).

you need to set the influence or weight for the bone actuator via python when initializing your agents - then copying the rotation will not prevent you from animating or configuring your model while viewing and working in the viewport. And set a zero value on the desired bones so that they are not a problem in the work

The constraint needs to be in the armature bone

Great, but it now unrealistically can go to 180 or 270 degrees in rotation, which in real life cannot occur without death occurring. Ive tried an Empty to limit this, but it still happens, even with Python. I want to limit the heads rotation in-game so it cannot exceed a certain limit similar to a human head in real life.

you can use the player local to figure out if the point is inside the ‘angle cone’ the head could reach

local = player.worldTransform.inverted() @ somePoint_inSpace
if local.x>0 and abs(local.y)-local.x<0 and abs(local.z)-local.x<0:
    #point is inside cone assuming X+ is player forward axis
else:
    #don't look at the thing

When I use the aforementioned worldTransform.inverted(), this pops up in the terminal: AttributeError: ‘Matrix’ object has no attribute ‘x’

Also, I discovered that if the thing spins too wildly around, it crashes the BGE with a segmentation fault. This is probably from overload.

Anyway, the thing is, how can I use worldTransform.inverted() without triggering the error?

In 0.3.0
Local = object.worldTransform.inverted() @ worldPoint

In 0.2.5
Local = object.worldTransform.inverted() * worldPoint

Did that. Says worldPoint is not defined. I think you mean worldPosition?

Any way to avoid the crash when the player turns its head towards the object?

Using players position to define the coordinates of the cone crashes the BGE, which is not cool.

worldPoint is the location of where a object is you want to see if it’s in the vision cone

like KX_object.worldPosition