Track to mouse cursor in 2d

Hey everyone.
To keep it short:
Can someone help me with creating a script or something else to let the green triangle track to the mouse cursor?
It is not 3d but 2d, so It should only rotate along the X axis in this case.
Screenshot:


What I want: (but I want this in realtime)


The .blend, in case if you want to make it for me:
tracktomousecursor.blend (504 KB)
I hope you can help me
Thanks!

Make a plane in background, do on it:
mose_over_any -> python --/
always -----------> and -------> track_to #second line is for the object that tracks to

Script(not the full, just the directions)


import bge

cont = bge.logic.getCurrentController()
targeter = bge.logic.getCurrentScene.objects["targeter"] #the object which points at the target
target = bge.logic.getCurrentScene.objects["target"] #empty with name target which is used for tracking

moa = cont.sensors["mouse_over_any"] #Sensors name here
act = cont.actuators["track_to"] #Put the actuator's name
target.worldPosition = moa.hit_position
target.worldPosition[1] = 0.0 #if you use X asis for movement
act.object = target

There may be mistakes in script as I done it here, in blenderartists, but oyu could try it anyway and fix it…

‘script placed in object to track to’
Note this assumes y axis is used for track to and x = points out of the screen



import bge
cont = bge.logic.getCurrentController()
own = cont.owner

MOA = cont.sensors['MouseOverAny']

if MOA.positive:
    own.alignAxisToVect(MOA.hitPosition-own.worldPosition,1,.5)
    own.alignAxisToVect(own.parent.worldOrientation.col[0],0,1)
    

Attachments

MOUSEAIM.blend (465 KB)

Thanks both!
The code of BPR works perfect. :wink:

There should be a way to do this without having to have the background plane to measure mouse position.

(brainstorm)
You can get mouse screen postion


from bge import logic
mouse_pos = logic.mouse.position

And the Player (or aiming arrow)'s screen position relative to the camera


player_pos = camera.getScreenPosition(player)

From those two points, one should be able to calculate the angle and set the orientation of the aiming arrow. I don’t know what that math is on the top of my head, though…

That way, you wouldn’t have to worry about having a giant invisible plane in the background of your game levels.

you just parent the plane to the camera :smiley:

the only nice thing about mouse over any, is things like targeting and showing text / healthbars etc.

that method looks pretty cool :smiley:

I use something similar in pies, to apply forces using the camera plane.