Move camera with mouse

Hey,

I am working on a First Person game now and I kinda stucked a bit on moving a camera.

So I assigned the camera to be the perspective of the view and I can move with W,A,S,D. But I want to make so I can look around, keeping the mouse in the middle. Also I might add that I am not too familiar with Python code.

Thanks,

Look at the tutorial on tutorialsforblender3d.com.

Worst tutorial ever, haha. Isn’t there just a script or something that I can include into a Mouse Movement sensor for the camera? Would be enough.

worst ha? maybe you shold look more carefully to that tutorial I think that works for the big majority of the users, I have problems when I use some specific filters, I ll not say that MouseLook.py is perfect, but is a big help.

What do you mean worst, it’s a great tutorial!!! Maybe you should look at it again, instead of just dismissing it.

Soz guys, I got it working now. I just did it wrong. My apology. :slight_smile:

Alternative: the MouseOrbiter. Just move the camera to the center and you have a mouse look.

I love the tutorial at TutorialsforBlender3d. It does everything I want it to do including caps and invert.

ya it the best one i found

Here’s the code for mouselook.

######################################################
#
#    MouseLook.py        Blender 2.45
#
#    Clark R Thames
#    Released under Creative Commons Attribution License
#
#    Tutorial for using MouseLook.py can be found at
#
#    www.tutorialsforblender3D.com
#                                                                                                         
######################################################


###########################  Logic Bricks

# Get controller
controller = GameLogic.getCurrentController()

# Get sensor named Mouse
mouse = controller.getSensor("Mouse")
   
# Get the actuators
rotLeftRight = controller.getActuator("LookLeftRight")
rotUpDown = controller.getActuator("LookUpDown")  


############################## Need the size of the game window

import Rasterizer

width = Rasterizer.getWindowWidth()
height = Rasterizer.getWindowHeight()
     

############################### Get the mouse movement

def mouseMove():
    
    # distance moved from screen center       
    x = width/2 - mouse.getXPosition() 
    y = height/2 - mouse.getYPosition()
   
    # intialize mouse so it doesn't jerk first time
    if hasattr(GameLogic, 'init') == False:
        x = 0
        y = 0
        GameLogic.init = True
   
    return (x, y)

pos = mouseMove()

   
######## Figure out how much to rotate camera and player ########

# Mouse sensitivity
sensitivity = 0.0020

# Amount, direction and sensitivity
leftRight = pos[0] * sensitivity
upDown = pos[1] * sensitivity

# invert upDown
upDown = upDown


######### Use actuators to rotate camera and player #############

# Set the rotation values
rotLeftRight.setDRot( 0.0, 0.0, leftRight, False)   
rotUpDown.setDRot( upDown, 0.0, 0.0, True)

# Use them
GameLogic.addActiveActuator(rotLeftRight, True)
GameLogic.addActiveActuator(rotUpDown, True)


############# Center mouse pointer in game window ###############

# Center mouse in game window
Rasterizer.setMousePosition(width/2, height/2)

So, you need to add 1 sensor, 1 controller, and 2 actuators. Make the sensor Mouse and set it to Movement, then name it “Mouse” (case sensitive). Go to the text window and open the script that I wrote above, and name it “mouselook”. Make the controller Python and set the script box to “mouselook” so it links with that script. Link the Sensor and controller together, and the controller to the two Actuators which are by default set on Motion. Leave the actuators on motion, rename the top one “LookLeftRight” and the bottom one “LookUpDown”. The order in which you name them, top to bottom or vice versa, doesn’t matter, just as long as their names are spelt correctly, case sensitive!

Also, you could just use 1 actuator on the camera naming it “LookUpDown” and select the camera and player object together, and add a Motion actuator to the player. Link the camera controller to the player actuator naming it “LookLeftRight”.

If your game involves clicking on objects to make them perform certain actions, you can add in:

Rasterizer.showMouse(1)

…into the code, which will show the mouse cursor. I hope everything goes well, cheers!

PS: The code on www.tutorialsforblender3D.com was written wrong, some values I found a long time ago were written with the negative and positive switched, so if you moved the mouse left, the camera would move right. I fixed it though in the code above! :smiley:

hi Riyuzakisan,

That code is really old. I wrote that MouseLook a couple of years ago and a lot of the python methods have been deprecated. You should update. The new code is for 2.49b.

BTW. The code wasn’t written wrong. Blender has undergone a lot of changes since it was written.

And hey guys, I’m glad you like the tutorial. Thanks for the nice things you said about it.

Clark

Heres a blend file I found a long time ago, it should help. Hope it does, I never tried taking it apart to find out.

Attachments

First Person Shooter.blend (919 KB)

This explains it too: http://www.gameblender.org/viewtopic.php?f=44&t=1280

@cthames: Oh I see, thanks! I wasn’t aware that there was a new code. I’ve visited your site before but I never took a look at the mouselook tutorial. :stuck_out_tongue: