Mouse follow

Hi guys,

I have another doubt which I need clarifying.

Basically, I’m trying to do an object follow mouse for a space invaders game. I am following this tutorial.

https://www.youtube.com/watch?v=f1qPW5WWlZU

But I noticed when I use mouse.hitPosition if i move the mouse cursor really fast the object no longer sticks to my mouse cursor.

So I thought maybe his script was buggy??? Originally I was just going to get the mouse coords to drop the object on the scene but realised the mouse coords are NOT the real blender game world coordinates (as I understand) so it wasn’t following the mouse but shooting off the scene.

Does anyone else get this behaviour and know how to resolve it thanks?

You could try using the “raySource” property on a mouse sensor instead. It sounds like you’re getting the hitPosition off the thing that you are actually moving, which would explain why it doesn’t follow your cursor anymore when you manage to move your cursor too fast. Alternatively you could put the hit position on an invisible box that covers the screen, then remove collision on everything else.

(I haven’t watched the video you posted so this is totally out of context with that)

You could try applying motion to the ship based on mouse motion, rather than setting the ship to the mouse’s hitPosition.

Basically, mouse-look but with translation instead of rotation!

Make your script look at mouse position, store it as ‘oldPosition’, then check it again the next frame. Take the difference between x positions (if you’re moving lef+right a la Space Invaders), and apply motion/force to the ship based on that difference. If this ship reaches either edge of the screen, clamp it’s movement.

(hypothetical code…don’t expect it to work as-is)


def mouseMove(cont):
own = cont.owner
m = logic.mouse

sens = 0.035    #mouse sensitivity factor
borders = (-50, 50)    #the BU value of the 'edges' of your screen
if 'oldX' not in own: own['oldX'] = 0

x = m.position[0]

own['oldX'] = (own['oldX'] + x) * sens

own.applyMotion([x,0,0], True)    #assuming we're sliding along the X axis

if own.worldPosition.x <= borders[0]:  own.worldPosition.x = borders[0]
if own.worldPosition.x >= borders[1]: own.worldPosition.x = borders[1]

own['oldX'] = x

is the actor to be locked on a plane?

you can use the mouse coords + the camera worldOrientation+ the plane depth instead of mouse over, then it will work even when you dont mouse over anything,