I have an empty and I have set a sphere as its child. When I change the empty's worldPosition(via right-clicking with mouse) the sphere teleports to entirely different location. The strange thing is that when I print the sphere's coordinates it is exactly the same as the empty's and the mouse cursor's. Any ideas?

Here is the module I wrote.

#Import the bge library.
import bge

#Make the mouse cursor visible during game running time.

#Set the current game scene and python controller to variables.
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()

#The function "initialization" is responsible for setting all scene objects, object sensors and object actuators to variables.
#These variables are created into the function but are for global use. Their names are the names of the objects/sensors/actuators in blender.
def initialization():
    for k in scene.objects:
    for k in ground.sensors:
    for k in cont.actuators:

def mouse_2D_coor():
    global mX, mY
    mouse_pos = bge.logic.mouse.position
    #Cramp the coordinates to what the camera sees. (0, 0) at the top left corner and (1, 1) at the bottom right corner.
    mX = max(0.0,min(1.0,mouse_pos[0]))
    mY = max(0.0,min(1.0,mouse_pos[1]))
    #print ("Mouse coor :",mX,mY)

def empty_to_pos():
    cursor_empty.worldPosition = (mop.position[0],mop.position[1],cursor_empty.worldPosition.z)

#print (mop.hitPosition)
if (right_click.positive):
    print (cursor_empty.worldPosition)
    print ("Mouse",mop.position)
    print ("sphere",sphere.worldPosition)

print ("I reached the end of track.py")

Here is the blend file


The answer came from the blenderartist's forum where the user agoose77 wrote

You need to use


. The position attribute of the base class of KX_MouseFocusSensor, SCA_MouseSensor, returns the pixel position of the mouse, which can reach a value in the thousands.

And it worked.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.