Aimable Turret

I need to make an amiable turret and I know what I need to do I’m just not sure how to actually go about doing it. By amiable I mean like the wrangler in team fortress 2 where the player isn’t near the sentry, but the sentry fires where the player is aiming. I believe the way to do this would be to have an object follow the mouse and have the sentry align its axis to the vector of it and the object. So I’d need to cast a ray between them and then alignAxisToVect it to get it to aim right. I’m just not quite sure how to go about that, any help you could give would be great, thanks!

Oh and I’ve got the object that follows the mouse working the way I want it too so don’t answer that

Ok well I’ve found out some stuff and I now have made *almost what I want. The only problem is that the turret still has rotation around its x-axis, and I’m not sure how to stop that from happening, my understanding of vector math is basic, verrry basic so I’ve just been guessing and checking practically. I only want it to rotate up and down and left and right, not sideways. I’ve tried taking out the x value in rori, and the y and z

ori = o.getVectTo(player)
rori = ori[1][0], ori[1][1], ori[1][2]

None of that worked, so I tried realigning the x axis every time it gets aligned to the object, but that didn’t work either

player.alignAxisToVect(rori, 2, 1)
player.alignAxisToVect([1,0,0], 2, 1)

I tried all three of the axis on this one too, anyone know how to block the x rotation?

you can try adding this in there:

xRot = own.worldOrientation.to_euler()    
xRot[0] = 0                                 
own.worldOrientation = xRot

that should keep the x rotation to 0

Sadly that won’t work, it’s the same as making rori’s x value 0 and that just breaks things. Here’s the blend if it’ll help

sorry I assumed you were getting vectors from your player object…This one works on mine

import GameLogic as GL
import bge
from Rasterizer import showMouse
from mathutils import Vector
cont = GL.getCurrentController()
o = cont.owner
player = GL.getCurrentScene().objects["Player"]
# Sensors
mouse_over = cont.sensors["mouse_over"]
leftmouse =
rightmouse =
keys =
hit_obj = mouse_over.hitObject
ori = o.getVectTo(player)

vec =  player.getVectTo(o)[1]

if mouse_over.positive:
   if hit_obj == o or == "Player" or == "Plane":
       o.worldPosition = o.worldPosition
       o.worldPosition = mouse_over.hitPosition
   if keys[leftmouse] >= 1:   
      if hit_obj:
         if 'grapple' in hit_obj:
            xRot = player.worldOrientation.to_euler()    
            xRot[0] = 0                                 
            player.worldOrientation = xRot

For some reason when I copied the lines needed from your script into mine, I got all these weird indentation errors. The script doesn’t have wrong indentation, at least not as far as I can see and I even went as far as to reset everything back to the left side and re-tabbed it. Still weird errors. Any ideas?

This usually happens when you use a mixture of tabs and spaces in your indentation. Python doesn’t like this and complains. Make sure that you consistently use only tabs or spaces in your indentation, not both (spaces are generally preferred).

Ok, I didn’t know that. I fixed it by retyping all the parts I had just copied from superflip but thanks for letting me know why this happens, I had assumed it was ok with either four spaces or a tab

Most code editors have an option that allows you to convert. In blender’s internal editor it is under text->whitespace->convert to [tabs, spaces]