Mouse control

Hi there!

Has someone an idea how to implement a “click & go” mouse control like in most of the rts and rpg games? What I mean: If I click somewhere in the environment then an object moves to that destination point. Moving is not the problem. But how to obtain the coordinates for the destination? I would be very grateful for any idea, hint, etc.


-Attach a Mouse-Movement Sensor to this script:

from GameLogic import *
from Rasterizer import *
from math import *

Cont = getCurrentController()
Own = Cont.getOwner()
Sens = Cont.getSensors()
Sensor = Sens[0]

Height = getWindowHeight()/2
Width = getWindowWidth()/2

get current mouse position

Xpos = Sensor.getXPosition()
Ypos = Sensor.getYPosition()

-Xpos and Ypos are mouse coordinates


Yep, I did that for an own mouse cursor. But how can I get the 3D-coordinates? That script returns the xy-coordinates in the active frame. How is it possible to calc the position in a 3D-environment e.g. the position in a room or on a landscape?

p.s.: Is it possible in the Blender game engine at all?

What I do in my game is to make a 3d cursur in the game. Moving the mouse up and down makes the 3d mouse move up/down, and so on with left and right. Then I just use a ray with “ray.getHitPosition()” to figure out the spot that you clicked.

Ah! Thanks. I’ll give it a try. Thanks for the help.

I just love this community, just as I discover that I have run into a snag, I come here to find that so has someone else and the answer has already been provided. Thanks guys.

Hmmmmmm… Not working the way I want it… :frowning: First I get only a x value which is pretty vague due to perspective projection, then I get the y value of the position of the object hit - but I don’t get any z value at all, only 0,000 jumping from negative to positive. Any ideas how to fix this?

ray.getHitPosition returns a list of 3 values, [x,y,z]. To access each element, you can do this:

x,y,z = ray.getHitPosition()

And then x will be the x position, y will be the y position, and z will be the z position.

If you like, you can download my strategy game early test at
and look at the mouse scripts.

Well, I’ve done the same, but it returns weird positions. Only the x position seems to be more or less correct, the y position is just the y position of the pivot point of the clicked object - no z at all, or let’s say: it’s 0.0000 or -0.0000 . Really weird. Ok, thanks for the link - I’ll take look!

there was a script in the original realease that did that. Thats what I’ve been using in my RPG and I’ve got it working ok. A warning though: that will only work when the cameras at position [0,0,0] and when its orientation is [0,0,0]. I modified it 2 work at and location and any rotation so that works fine 4 me now. At d moment Im working on getting the player to walk round obstacles in his path and continue his motion towards the predefined stop point. 8)

Do you have that demofile? Can’t find it. Only the 2.11 and newer, or 2.0 and older.

I think ive got the original file at home somewhere. I’ll have a look for you. I specifically mentioned the file cos i remember the mouseover.blend file wasn’t included in the or later for some reason. :-?

Oh! I would be very grateful! Yes, it isn’t included in the demos I have. I’ve been searching. But I also can’t find demo211…

i made this a while ago just to see if it was possible, i think its what you want

Thanks. Well, slowly I’m getting there, I think. Thanks saluk and you. (Nice laser dot, btw.) :slight_smile: I’ll give it a try once more - it just has to work! :wink:

For rank beginners like me, I think these will show the basic theory better:
See the boxmouse.blend link for basic movement, and boxmouse3.blend for selecting and moving one of three objeccts.

Two problems exist: position flicker when moving objects (should I use actuators instead of setPosition?) and loss of mouse control when the real cursor reaches the edge of the window.
Is there a way to constantly reset the 2d cursor position to the center of the window to avoid the second problem?

why don’t you just use a mouse over and mous left click both connected to a AND controller instead of using a ray?

First I use an own cursor, second: That doesn’t return the coordinates on an object - or shorter: It doesn’t work for me.

The script I use uses the Rasterizer (spelling?!) module to get the position of the real mouse cursor, and it uses that to set the position of the blender cursor. So the problem of the real mouse cursor reaching the top of the screen is gone because when the real mouse cursor reaches the top, the blender cursor does as well.

Keith. 8)

Hey, i am doing a similar thing, and I read a lot of posts, but i was wondering if you could point me at a link (for some reason the ones already in this thread dont work for me). Yeah, i know i am new to blender/python and that i should try stuff first but since i was browsing and found the question/answer i am looking for i thought i might as well take advantage of the situation. Any help would be very appreciated, thanks.