Adding Object to the Mouse Clicked Area

Hi, I am using blender for 4 years but I have started coding in blender using python lately. I am new to Python but I know C and C++. Anyway here is my question:
I’ve basic scene with a plane and a camera. Plane’s view is aligned to camera. I need a script which creates the specified object in the point player click.I wrote this code:


import bge
G = bge.logic
cont = G.getCurrentController()
scene= G.getCurrentScene()
owner = cont.owner
mouse= cont.sensors["Mouse"]
owner.position=[mouse.position[0],mouse.position[1],1]
ob = scene.addObject("Obj",owner)

But the mouse.position command get the position of the mouse from camera view so it is not global.So how can I make the code work right. How can I get what I want? Please help.I don’t know what to do.

Thanks :eyebrowlift2:

use a mouseoverany sensor, get its hit position in combination with mouse left click sensor.

then u get:

controller      = bge.logic.getCurrentController()
scene   = bge.logic.getCurrentScene()
mouse           = controller.sensors["Mouse"] #left click
mouseover       = controller.sensors["Mouseover"] #mouseover(any)
hitpos          = mouseover.hitPosition


    if mouse.positive and mouseover.positive: #if mouse click + over is true
        scene.addObject("Obj",hitpos)

no idea if this is what u want, but it works for me :slight_smile: