EASY MouseLook Script for BGE 2.5!

Finally, an easy mouseLook script created by me.
Only 30 lines of code - yay!

import bge

cont = bge.logic.getCurrentController()
obj = cont.owner
mouse = cont.sensors["Mouse"]
leftRight = cont.actuators["Motion"]
upDown = cont.actuators["Motion1"]

width = bge.render.getWindowWidth()
height = bge.render.getWindowHeight()
pos = mouse.position
xPos = int(width/2) - pos[0]
yPos = int(height/2) - pos[1]

leftRight.dRot = [0.0, 0.0, xPos*0.0005]
leftRight.useLocalDRot = False
upDown.dRot = [yPos*0.0005, 0.0, 0.0]
upDown.useLocalDRot = True
cont.activate(leftRight)
cont.activate(upDown)

if not mouse.positive:
    xPos = 0
    yPos = 0

if pos != [int(width/2), int(height/2)]:
    bge.render.setMousePosition(int(width/2), int(height/2))
else:
    cont.deactivate(leftRight)
    cont.deactivate(upDown)

You my friend, are a genius:D does this code work in 2.57, the latest build? I sure hope so.

Yes, it does work in any 2.5X~, this is how I designed it! :slight_smile:

Awesome! One thing I’m noticing Is that u can’t adjust the mouse sensitivity right in the script, is there another way to do this? I mean change mouse sensitivity without adding to the code?

Change the bolded numbers



leftRight.dRot = [0.0, 0.0, xPos*<b>0.0005</b>]
leftRight.useLocalDRot = False
upDown.dRot = [yPos*<b>0.0005</b>, 0.0, 0.0]
upDown.useLocalDRot = True
cont.activate(leftRight)
cont.activate(upDown)


And learn to just try things out sometimes.

Here’s a version that’s easier for beginners
You can change the sensitivity with a variable

import bge

cont = bge.logic.getCurrentController()
obj = cont.owner
mouse = cont.sensors["Mouse"]
leftRight = cont.actuators["Motion"]
upDown = cont.actuators["Motion1"]

sensitivity = 0.0005

width = bge.render.getWindowWidth()
height = bge.render.getWindowHeight()
pos = mouse.position
xPos = int(width/2) - pos[0]
yPos = int(height/2) - pos[1]

leftRight.dRot = [0.0, 0.0, xPos*sensitivity]
leftRight.useLocalDRot = False
upDown.dRot = [yPos*sensitivity, 0.0, 0.0]
upDown.useLocalDRot = True
cont.activate(leftRight)
cont.activate(upDown)

if not mouse.positive:
    xPos = 0
    yPos = 0

if pos != [int(width/2), int(height/2)]:
    bge.render.setMousePosition(int(width/2), int(height/2))
else:
    cont.deactivate(leftRight)
    cont.deactivate(upDown)

Amazing. Thank you sir.

I have no idea whats wrong with my blender grrrrrrrr

@jovan - … Do you mean your literal blender, or do you mean Blender, the program?

The script when i put run script it states, python script fail look at console for now and import bge is highlighter, sorry im new :frowning:

Sorry, I was a bit insensitive. No problem. You don’t run the script via the ‘Run Script’ button - you put the script into a text file, and then plug that file into a Python controller. Connect an Always sensor to that controller, click the ‘True Pulse’ button (the one that looks like this: [’’’] ), and it should work.

I did that but it doesn’t work? What am I doing wrong? Here’s the file: mouselook-script.blend (399 KB)

I did the logic on the camera, in case that matters.

Here is how you should acheive this:
Mouse movement sensor called “Mouse”
Two motion sensors (click add, don’t name them)
connect them to the script, no need for always

Attachments

mouselook-script.blend (363 KB)