I like to treat all inputs the same, so if I’m making a small game and using logic bricks for input, I’ll use logic bricks for the mouse.
If I’m using a method to map keys to controls in python (eg a keymapper, complex GUI’s etc) then I’ll directly access the mouse.
Both ways exist, and both are useful. Pick one and see if it fits what you are doing with it.
Here is the script set-up I have used for a good-feeling mouse-look.
return (render.getWindowWidth(), render.getWindowHeight())
window = get_window()
return (window//2, window//2)
center = get_window_center()
own = cont.owner
m = cont.sensors['mouse_move']
head = own.children['Head']
sensitivity = own['mouse_sensitivity'] * 0.01
smooth = own['mouse_smooth']
if 'oldX' not in own:
center = get_window_center()
own['oldX'] = center
own['oldY'] = center
source = Vector(get_window_center())
mpos = Vector(m.position)
x = source.x - mpos.x
y = source.y - mpos.y
own['oldX'] = own['oldX'] * smooth + x * (1.0 - smooth)
own['oldY'] = own['oldY'] * smooth + y * (1.0 - smooth)
x = own['oldX'] * sensitivity
y = own['oldY'] * sensitivity*(render.getWindowHeight() / render.getWindowWidth())
ori = head.localOrientation.to_euler()
if ori.x <= -0.6:
ori.x = -0.6
elif ori.x >= 0.6:
ori.x = 0.6
head.localOrientation = ori
Thanks to Mike Pan for the original code.
The script goes on your presumed FPS rigs ‘body’ (the ‘head’ being either a child object which has the camera childed to that, or the head could just be the camera).
To Set Up:
-Name your camera child ‘Head’
-Give the body (the object that’s running this script) the following properties as floats:
To make the script work, noodle its controller to a Mouse sensor set to Movement. The python controller should be on Module mode set to ‘mouse.look’, if you name this script mouse.py
My guess would be that you were trying Mike’s original script as-is. Which, if I remember correctly, written for ancient Blender whose language has since been changed. Thus, I took the original script and ‘modernized’ it.
Seemed broken how? What does the debug console say? If you’re trying to do anything with scripts without a debugger, you are bound to find yourself with many head-aches
Try this one. This works in 2.76.2. I just combined mouse and keyboard in one script but I didn’t include the rotation cap and smoothing.I made this one because other scripts have too many extra complications/requirements/dependencies. This one needs only an Always sensor in pulse mode.
width = bge.render.getWindowWidth()
height = bge.render.getWindowHeight()
nWidth = (width // 2) / width
nHeight = (height // 2) / height
scene = bge.logic.getCurrentScene()
keyboard = bge.logic.keyboard
mouse = bge.logic.mouse
center = bge.render.getWindowWidth() / 2, bge.render.getWindowHeight() / 2
active = bge.logic.KX_INPUT_ACTIVE
activated = bge.logic.KX_INPUT_JUST_ACTIVATED
rmb = bge.events.RIGHTMOUSE
w = bge.events.WKEY
s = bge.events.SKEY
a = bge.events.AKEY
d = bge.events.DKEY
lsh = bge.events.LEFTSHIFTKEY
cont = bge.logic.getCurrentController()
obj = cont.owner
rotscale = 0.8
speed = 24
x = mouse.position - nWidth
y = mouse.position - nHeight
if keyboard.events[w] == active:
if keyboard.events[s] == active:
if keyboard.events[a] == active:
if keyboard.events[d] == active:
if (keyboard.events[lsh] == active
and keyboard.events[w] == active):
Problem is, the capping code from Mike Pan mouselook script is doing some weird stuff when you rotate the camera quickly around Z.This code is also doing the same buggy stuff. Can anybody figure out why is not working properly the capping code or the rotation limiter ?