Hi,
Here is some ways to use the mouse.
First off you might want to make the mouse visible, you can do this like so:
i
mport Rasterizer
Rasterizer.showMouse(1)
If you want to use the mouseās movement to do something then make a mouse sensor set to movement.
Now in a python code do this:
cont = GameLogic.getCurrentController()
own = cont.getOwner()
mouse = cont.getSensor("mouse")
mouse_x = mouse.getXPosition()
mouse_y = mouse.getYPosition()
mouse_x and mouse_y are variables that have the x and y values of the mouseās position in them.
If you want to move an object using the mouse you use a mouse sensor set to mouse over and a left button mouse sensor.
The mouse over has a nice little function called .getHitPosition(), this returns the x,y and z values of where the mouse is over the object (in global co-ordinates).
So, if we want to move a cube around in a scene, we add the cube and add 3 sensors, an always, a mouse over and a mouse left click.
Then by using the .getHitPosition() values and setting the cubes position to those we can make a mouse movement script, but this makes a little problem as the cube will always move towards the camera because it will hit the side of the cube which is +1 of the centre of the cube so when its position is set to the hit position it will always be moving towards the camera by +1.
Hope I didnāt lose you there, if I did donāt fret it, its not too important.
So if we make a script to do this it could look like this:
import Rasterizer
Rasterizer.showMouse(1)
cont = GameLogic.getCurrentController()
own = cont.getOwner()
over = cont.getSensor("over")
lClick = cont.getSensor("lClick")
if over.isPositive() and lClick.isPositive():
pos = over.getHitPosition()
own.setPosition([pos[0],0,pos[2]])
This pretty much checks to see if the mouse if over the cube and if the left mouse button has been clicked. If both those conditions are true then then it assings the hit position to a variable called āposā, then we move the cube by using 'own.setPosition([x,y,z]).
As you can (or perhaps cannot, Iām not too sure about your strengths in python) the x position that we set the cube to is pos[0], this is the x value of the hit position thingo. But the y value is 0, this is our work around to the cube flying towards the camera and then finally the z value is the z value of the hit position.
Lastly add a script controller and connect it to that script by putting āTextā in the script field (or whatever you may have called it) and connect all 3 sensors to that and hit P and have fun dragging the cube.
Just for you I have made an example of this (attached), if you have any questions on bits you didnāt understand just say so and Iāll go through it more detailed.
And also, if this doesnāt help tell me why and what your try to achieve with the mosue.
Attachments
drag example.blend (123 KB)