Detecting Z-Coord of Object Under Mouse

The title is a little misleading, as what I really want is to detect only the part of the object the mouse is over. So, I might have a slanted plane, and want to pass a mouse along it. As the mouse passes over it, some script would determine the Z-Coord of just that part of the plane the mouse is currently over. If this were output to a property printed in Debug, you might see “testob.z-coord” start at 0 and count up (and down) as the mouse passed over the plane, detecting just the part of the object that the mouse is over.

I’m pretty sure I restated the same thing 2 or 3 times there. But you get the idea. :stuck_out_tongue:

The purpose of this script is to place building objects on-mouse-click along a ground model like you see in games like Dawn of War or Star Craft.

SOLVED - credit to SolarLune

I use a cube (arbitrary name) with a Float Property “Z”. Then a Mouse Over Any sensor (named MouseOverAny) with True-Level-Triggering, tied to your python script. The script is as follows:

from bge import logic
cont = logic.getCurrentController()
own = cont.owner
mouseoverany = cont.sensors[“MouseOverAny”]
own[‘Z’] = mouseoverany.hitPosition.z

rom bge import logic as gcnt=g.getCurrentController()mouseoverany=cnt.sensors[“MouseOverAny”]


I’m pretty new to Python scripting for the BGE, so I’m not entirely sure what to do with those. I assume the first would be on some second object that receives the z-coord data. The second script, though, I’m not sure where it’d go.

Use MouseSensor.hitPosition rather then hitObject.

It is a wild guess, but i think that is hat you want.

The script, as I’m using it, looks like this currently.

** edit - Monster’s hitPosition

rom bge import logic as g

Not working.

Currently I have a Plane and a Cube.

The Cube has a property called Z (float) and a Sensor named MouseOverAny attached to this python script

rom bge import logic as g
own.get("Z") = (mouseoverany.MouseSensor.hitPosition.z)

Looking at it a second time, I’m realizing it doesn’t matter what the script is attached to (as long as it’s the object you want tracking Z). So having two objects is needless. But, anyway. Yeah. Notta.

**looking up this mysterious MouseSensor.

This should help. The sensor you get from the cont.sensors list is stored in mouseoverany, as you have above, but you don’t have to use ‘get()’ - you can just set the variable with own[‘Z’]. Also, mouseoverany is the sensor itself, so there’s no such thing as “mouseoverany.MouseSensor”. You can check out the BGE API here.

from bge import logic

cont = logic.getCurrentController()

own = cont.owner

mouseoverany = cont.sensors["MouseOverAny"]

own['Z'] = mouseoverany.hitPosition.z

Thanks a lot. I just found the hitPosition tutorial, too. Lots of good info. Again, thanks.