Wednesday, September 23, 2009


Another tutorial posted by Ninja Goliath on the Blenderartists forum.

How do work the famous "Mouselook"
script used in most blender games ?

first of all, here are The basics of MouseLook :

The MouseLook script first involve, of course, the mouse ! !

but how ?

simple : while in-game, when you move your mouse, it's actually really moving
the mouse right in front of you. You just can't see it cuz it's hidden by default.

but if you could see it, you would see it stick pretty much at the middle of
your screen because the Mouselook code do Try to stick your mouse at the middle
of your screen every 1/60 seconds.

Thus, during 1/60 second, you can actually move your mouse from a few pixels.
If you do so, during the next "game engine computation" = after about 1/60 seconds,
the mouselook script will look at your mouse's position and say :

0.5 - I musn't jerk !
1 - OMG !!!
2 - The mouse isn't at the middle of the screen !!!"
3 - I must rotate the camera from a corresponding value !! ARGARGRG !!!
4 - Put back this cursor at middle of the screen !!!

And that's exactly how every single mouselook of the entire community works (AFAIK)
They just use different syntax and differents way of codeing it.
but OVERALL, they're pretty much all the same.

K, now you probably want to know how the heck you code this hein ?
Well here we go :

STEP 0.5 : Don't jerk plz !

This "0.5" step isn't really a step since it's only there
to put the mouse in the middle of the screen the first time the script run.

(SO that your screen doesn't jerk or so.)
(Delete that part of code and you'll quickly see why it's quite required :wink: )

The associated code is :

STEP 1 : OMG !!!

This is the common initialization of a script. It is the way
to include the sensors/actuator/owner object/properties into your code

Notice we import the "Rasterizer" module. (At line 8 )
This is to be able to use the mouses related functions
(to get the mouse position in our case and to stick it at the middle of the screen)

The above script will need the following setup (At least) to work
Because if it can't find a property/sensor/actuator it's looking for,
the script will crash in your face.

Notice that, in this script, we also set the "Mouse sensitivity".
This is the constant used to reduce the rotation (In number of pixels)
gathered from the mouse's position into a realistic amount of rotation.

STEP 2 : Where's the mouse ??

This step involve 2 different parts of codes :

The related function :
(Note that it's where the "init script" is contained.)

The related code :

The line "#86" involve the "moseMouve()" function which look for the
mouse's position and store it in the variable used to call the code
(the info is stored into the "move" variable in our case)

The lines 89 & 90 calculate the x & z rotation from the informations
stored into the "move" variable and with the mouse sensitivity.

STEP 3 : Rotate the camera ! ARGARGRG !!!

Here is the quite simple script who take care of this :

For more info, you can alway look into the API for the "applyRotation" function.
It's really simple as you can see.

I would only specify that we use the prefix "owner.parent" for the Z axis's rotation
instead of the common "owner" relating to the fact that, in the code, the owner is
actually the camera and it's parent is the player. Thus, the player only
rotate to left and right (Z axis) and the camera rotate up-down (X axis).

STEP 4 : Put back the cursor at middle of the screen !!!

That's all :P

Here's the overall code :

my own code is pretty much the exact
same as this except that it "CAP" the camera.

& there's a tricky setup so that the camera move smoothly.


Anonymous said...

Hi, great tutorial but one thing is when I do 'run' (Alt+P), it says 'python script error: check console' and highlights line no. 74. I have checked and rechecked and I definitely have it copied character for character, so perhaps there is something in need of replacement on that line? However, this may be due to the fact that I'm using 2.49b instead of 2.49a, but I doubt it. any help appreciated!

Anonymous said...

I have a problem too. On Blender 2.5 Alpha 2 it says that there is no module named Rasterizer. It doesn't work. I tried it on 2.49b and it doesn't work there either. What am I doing wrong?

Anonymous said...

Blender 2.5 has different codes syntaxes and functions, scripts that run on blender 2.4 series will not run on 2.5, PERIOD...