I am making a third person shooter. I have a character with a gun that can shoot, and the armature has a track-to constraint to an empty. I followed some of Mattline’s dev logs on youtube, and he mentioned that to make his character always shoot to the center of the camera, where my cursor is, he shoots a ray from the camera and places the target empty wherever it hits. I was wondering how to do this: shoot a ray and place an object where it hits.
Check this out
import bge def main(): cont = bge.logic.getCurrentController() own = cont.owner sens = cont.sensors['Ray'] sens2 = cont.sensors['Keyboard'] sens3= cont.sensors['Property1'] if own['State']==1: stock=own['Block'] type="Block" if own['State']==2: stock=own['Block2'] type="Block2" if own['State']==3: stock=own['Block3'] type="Block3" if sens.positive and sens2.positive and sens3.positive and stock>0: hitPos=own.worldPosition.copy() hitPos.x=sens.hitPosition hitPos.y=sens.hitPosition hitPos.z=sens.hitPosition diff=own.worldPosition-hitPos Dist=diff.magnitude if Dist>2: hitObPos=sens.hitObject.worldPosition.copy() scene=bge.logic.getCurrentScene() add=scene.addObject(type,own,0) Norm=own.worldPosition.copy() Norm.x=sens.hitNormal Norm.y=sens.hitNormal Norm.z=sens.hitNormal hitObPos=hitObPos+Norm add.worldPosition=hitObPos add.alignAxisToVect(sens.hitNormal, 1, 1) add.setParent(sens.hitObject,0,0) add.applyMovement((0,1 ,0),1) if own['State']==1: own['Block']+=(-1) if own['State']==2: own['Block2']+=(-1) if own['State']==3: own['Block3']+=(-1) main()
@Blueprint, that is extremely long, and may do some other things? Here’s my version, which requires to be run by whatever object you want to aim at the center (don’t need an empty)
import bge own = bge.logic.getCurrentController().owner camera = bge.logic.getCurrentScene().active_camera cast_to = camera.worldPosition + camera.getAxisVect([0,0,-1]) _, point_to_aim_at, _ = camera.rayCast(cast_to, camera, 100) if point_to_aim_at != None: current_point = own.worldPosition aim_vector = current_point - point_to_aim_at own.alignAxisToVect(aim_vector, 1, 1) #comment this next line out, when you don't want the green lines bge.render.drawLine(current_point, point_to_aim_at, [0,1,0]) else: aim_vector = camera.getAxisVect([0,0,-1])
Now been tested, here’s a blend:
aimatcenter.blend (552 KB)
I’m can’t use python, so I don’t understand any of that. Sorry. Could you make me a script based on this data?
The target empty’s name = Target
The camera that’s shooting the ray’s name is : Camera
I need for the camera to shoot a ray and the target to appear wherever the target hits, and this happens always, or on pulse.
what logic actuators do I need to put into the python? and does it need to be a module?
Why can’t you use Python? =s
If you want to use a point in a ray to do this, you HAVE to use Python =/
Well, its not that I can’t use Python, I just don’t know how to use it. Script, please?
Thanks! It worked!
Better to get started sooner than later with Python (speaking from experience). Learning Python was like becoming a born-again Blenderer.
If you really can’t use Python, I recommend using the Mouse Actuator which can only be found in a Daily Blender build (highly experimental – use with caution) which can be found here: https://builder.blender.org/download/
I’m gonna try to learn python better now, theres only so far I can go asking people for programming help here. Thanks for the advice.