# Bge Python: Move objects along one axis using mouse movement

I don't have experience on python. I want move object and move something like this: How to make it works like that ? I don't have much time to learn all python commands because i just want it simple. Thank you.

Simple script, need more work but its good start.

from bge import logic
logic.mouse.visible = True
def main(cont): #cont = currentController
cont.owner.worldPosition.x = logic.mouse.position[0]**2 # owner likely the cube


just add a cube and in logic nodes add always sensor + python controller.

EDIT

"logic.mouse.visible = True" show the mouse when bge its runnig so you can see the mouse position on screen. For clarify: my script name is "script.py" so im using python controller to run a module name main from my script ("def main()"), so every tick it run main().

You can avoid this setin python controller to run script and not module:

and the script should be like this:

    from bge import logic
logic.mouse.visible = True
def main(cont): #cont = currentController
cont.owner.worldPosition.x = logic.mouse.position[0]**2 # owner likely the cube
main()


OR just this:

from bge import logic
cube = logic.getCurrentController().owner
cube.worldPosition.x = logic.mouse.position[0]**2


so this script NEED MORE WORK, to get it work like you want you need to work on it, and dont need to learn a bunch of things.

And for the last tip. mouse position go from 0.0 to 1.0 and when mouse position its > 0.5 will go to the left of the screen (your right) and the other way if its < 0.5. Finally if you show your work and not just and example without trying yourself to solve it, i cant help you anymore because the idea its to you learn something and not just copy.

• why you need "logic.mouse.visible = True"? Why you need a function generic name? You can use script mode instead and get rid of that confusing construct. – Monster Feb 23 '17 at 7:55
• Err… doesn't work… I still confused, using python module instead script on that controller? I usually not use that, and also, no mouse movement sensor ? Without that it might can't control the cube… or maybe I missing something ? Are you sure that is work ? – palid Feb 23 '17 at 8:54
• Okay. The third simple one works. Now Ive got what I want. I don't change anything but I just set the camera location though, removed double asterisk and change it to 5 instead of 2. Sorry then, I'm just afraid. Well, accepted. – palid Feb 24 '17 at 2:52
• ** is the operator for "power of". It`s just a finetune. let say that ´mouse.position[0] = 0.2345 **´ 2 would be 0.05499025 and by ** 5 = 0,00070911102104215625 so the cube would move less. and removing ** and leaving the 5 will generate an error. but u can use mouse.postion without **N – Strapicarus Feb 24 '17 at 7:05