Track to mouse in side view


I working on a 2d minigame where your can move around platforms and shoot at ennemies

But I can’t find a good “track to mouse” script, because all of them work only in top view and mine is in side view, and so the gun only rotates around Z axis
I want it to rotate around X axis when you move the mouse

I can upload the blend file if you want, just ask

Hope someone can help me


That should be really simple to fix. Just change the axis you want the gun to rotate around in the script.

well actually I don’t see any mention of axis in the script

that’s the script :

import GameLogic

con = GameLogic.getCurrentController()
own = con.getOwner()

mouseover = con.sensors[“sensor”]

own.position = mouseover.hitPosition

if you could help me because I can’t understand Python code

This script looks like it is moving an object around under where the mouse is (own.position = mouseover.hitPosition). I would create an empty and attach this code to it. Then track the gun or the arms of your character towards the empty.

I’ve also tried it but it still doesn’t work…

Here’s the blend file if you wanna have a look :
2D Shooter game.blend (199 KB)

Thanks for your quick answers, and hope the file will help

Here is a working demo of what you want.

sideways_mouse_track.blend (362 KB)

Frankly your file was too confusing for me, with all the weird rotations.

The one thing to watch is that the gun will rotate at different speeds depending on where the empty is. If you zoom back you can see the object parented to the empty. The farther away the empty the slower the rotation of the gun.
You could probably get around this by constraining it with .getDistanceTo() function, though.

P.S. Even from just looking at the very beginning of your game it looks like it could be really fun. I look forward to seeing it in the finished games section of the forum.

Thanks Infininte !

Actually your demo works… but not mine if I copy the script and the logic bricks… nothing happens, the empty doesn’t seem to move with the mouse…

It seems that the script isn’t working with me…

I’m really lost, I’ve tried everything… I think I’ll just learn Python and write the code

Hmm… Yes, your file was doing some weird things for me. I would have done the script for your file but I just couldn’t understand what was going on.
I’ll give it a try.

Alright I got it working.

2D Shooter game.blend (281 KB)

The biggest difference is I was using a Y,Z set up and you had a X,Z set up.
I also changed the always sensor that tracks the gun to the empty. You had it with a frequency. You also had the time set on the track to actuator I changed that to zero. With no frequency set the always sensor fires once. With no time set the track to sensor will always track to the object.