Move the view in game by moving mouse?

Hi all, I am a Blender user of 3 years but I just started creating my first game 3 days ago :smiley:
I tried figuring it out myself, I googled it with no luck and searched forums but anyways, I can’t figure out how to make the view rotate around the certain object when mouse is moved in that direction? (As in CS for example). I tried playing around with logic editor with no luck. Is there a certain code for it or is it just a matter of logic bricks? The closest i could find was rotating the view in a single direction when mouse was moved at all by connecting mouse movement and motion bricks.
Any tips? :confused:

The simplest way to do this is with an animation. If you animate the camera rotating in either direction, you can then use logic bricks to control it in either direction.
Either that or it is pretty simple to write a code with parenting.
Another option, which would be my personal choice, would be this:
-Shift+S and cursor to selected
-Create a cube
-Set the object you want the camera to rotate around to be the cube’s parent
-Set the cube to be the camera’s parent
-Use logic bricks in any way you see fit to rotate the cube, and the camera will follow in whatever set direction.
With the parent/child relationship, you can move the camera without affecting the cube, move the cube without affecting the object, but affecting the camera, and move the object and affecting all three.

I’m fairly new to python so this might be messy, but try this out.
http://www.pasteall.org/blend/27371

http://www.tutorialsforblender3d.com <- Love this site.

Mouse-Look Script -> http://www.tutorialsforblender3d.com/Game_Engine/MouseLook/MouseLook_First_1.html

If that one doesn’t work, go back to the home page and look for the Tutorials section there for 2.6.

This is a very nice simple setup. Unfortunately, it seems that there is a bug with setting mouse position to 0.5.
It sets it at .49915966, so the camera keeps moving very slowly

Dunno what your setup looks like, but make sure your script is set to only move the camera when the mouse is actually moving.

Taken from a TutorialsForBlender3D tut I did a while ago.

  1. Add your character and camera to the scene.
  2. Add a Mouse sensor to your camera and set it to movement (no pulse checking). Name it MouseLook.
  3. Add a controller, type Python.
  4. Add two Motion actuators, Simple Motion. Name the first UpDown and the second LeftRight.
  5. Connect MouseLook to your controller, and your controller to both UpDown and LeftRight.
  6. Create a new text file and set your controller to point to it.
  7. Paste the following script into your text file
######################################################
#
#   MouseLook.py        Blender 2.55
#
#   Tutorial for using MouseLook.py can be found at
#
#    [www.tutorialsforblender3d.com](http://www.tutorialsforblender3d.com)
#
#   Released under the Creative Commons Attribution 3.0 Unported License.    
#
#   If you use this code, please include this information header.
#
######################################################


# define main program
def main():
    
    # set default values
    Sensitivity =  0.002
    Invert = 1
    Capped = True
    
    # get controller
    controller = bge.logic.getCurrentController()
    
    # get the object this script is attached to
    obj = controller.owner
    
    # get the size of the game screen
    gameScreen = gameWindow()
    
    # get mouse movement
    move = mouseMove(gameScreen, controller, obj)
    
    # change mouse sensitivity?
    sensitivity =  mouseSen(Sensitivity, obj)
    
    # invert mouse pitch?
    invert = mousePitch(Invert, obj)
    
    # upDown mouse capped?
    capped = mouseCap(Capped, move, invert, obj)
    
    # use mouse look
    useMouseLook(controller, capped, move, invert, sensitivity)
        
    # Center mouse in game window
    centerCursor(controller, gameScreen)
    
#####################################################


# define game window
def gameWindow():
    
    # get width and height of game window
    width = bge.render.getWindowWidth()
    height = bge.render.getWindowHeight()
    
    return (width, height)


#######################################################


# define mouse movement function
def mouseMove(gameScreen, controller, obj):


    # Get sensor named MouseLook
    mouse = controller.sensors["MouseLook"]


    # extract width and height from gameScreen
    width = gameScreen[0]
    height = gameScreen[1]


    # distance moved from screen center
    x = width/2 - mouse.position[0]
    y = height/2 - mouse.position[1]
    
    # initialize mouse so it doesn't jerk first time
    if not 'mouseInit' in obj:
        obj['mouseInit'] = True
        x = 0
        y = 0
    
    #########    stops drifting on mac osx
       
    # if sensor is deactivated don't move
    if not mouse.positive:
        x = 0
        y = 0
    
    #########  -- mac fix contributed by Pelle Johnsen
                    
    # return mouse movement
    return (x, y)




######################################################


# define Mouse Sensitivity
def mouseSen(sensitivity, obj):
    
    # check so see if property named Adjust was added
    if 'Adjust' in obj:
        
        # Don't want Negative values
        if obj['Adjust'] &lt; 0.0:
            obj['Adjust'] = 0.0
        
        # adjust the sensitivity
        sensitivity = obj['Adjust'] * sensitivity


    # return sensitivity
    return sensitivity


#########################################################


# define Invert mouse pitch
def mousePitch(invert, obj):
    
    # check to see if property named Invert was added    
    if 'Invert'in obj:
            
        # pitch to be inverted?
        if obj['Invert'] == True:
            invert = -1
        else:
            invert = 1
            
    # return mouse pitch
    return invert


#####################################################


# define Cap vertical mouselook
def mouseCap(capped, move, invert, obj):
    
    # check to see if property named Cap was added
    if 'Cap' in obj:            
    
        # import mathutils
        import mathutils
        
        # limit cap to 0 - 180 degrees
        if obj['Cap'] &gt; 180:
            obj['Cap'] = 180
        if obj['Cap'] &lt; 0:
            obj['Cap'] = 0
        
        # get the orientation of the camera to parent
        camOrient = obj.localOrientation
        
        # get camera Z axis vector
        camZ = [camOrient[0][2], camOrient[1][2], camOrient[2][2]]
        
        # create a mathutils vector 
        vec1 = mathutils.Vector(camZ)
        
        # get camera parent
        camParent = obj.parent
        
        # use Parent z axis 
        parentZ = [ 0.0, 0.0, 1.0]
        
        # create a mathutils vector
        vec2 = mathutils.Vector(parentZ)


        # find angle in radians between two vectors
        rads = mathutils.Vector.angle(vec2, vec1)


        # convert to degrees (approximate)
        angle = rads * ( 180.00 / 3.14) 


        # get amount to limit mouselook
        capAngle = obj['Cap']
                
        # get mouse up down movement
        moveY = move[1] * invert
        
        # check capped angle against against camera z-axis and mouse y movement
        if (angle &gt; (90 + capAngle/2) and moveY &gt; 0)   or (angle &lt; (90 - capAngle/2) and moveY &lt; 0)  == True:
            
            # no movement
            capped = True


    # return capped
    return capped


###############################################


# define useMouseLook
def useMouseLook(controller, capped, move, invert, sensitivity):
                
    # get up/down movement
    if capped == True:
        upDown = move[1] * sensitivity * invert
    else:
        upDown = move[1] * sensitivity * invert
        
    # get left/right movement
    leftRight = 0
        
    # Get the actuators
    act_LeftRight = controller.actuators["LeftRight"]
    act_UpDown = controller.actuators["UpDown"]  
    
    # set the values
    act_LeftRight.dRot = [ 0.0, 0.0, leftRight]
    act_LeftRight.useLocalDRot = False  
    
    act_UpDown.dRot = [ upDown, 0.0, 0.0]
    act_UpDown.useLocalDRot = True
    
    # Use the actuators 
    controller.activate(act_LeftRight)
    controller.activate(act_UpDown) 
    
#############################################


# define center mouse cursor
def centerCursor(controller, gameScreen):
    
    # extract width and height from gameScreen
    width = gameScreen[0]
    height = gameScreen[1]
    
    # Get sensor named MouseLook
    mouse = controller.sensors["MouseLook"]
    
    # get cursor position
    pos = mouse.position
        
    # if cursor needs to be centered
    if pos != [int(width/2), int(height/2)]:
        
        # Center mouse in game window
        bge.render.setMousePosition(int(width/2), int(height/2))
        
    # already centered.  Turn off actuators
    else:
        # Get the actuators
        act_LeftRight = controller.actuators["LeftRight"]
        act_UpDown = controller.actuators["UpDown"]  
        
        # turn off the actuators 
        controller.deactivate(act_LeftRight)
        controller.deactivate(act_UpDown) 


##############################################


#import GameLogic
import bge


# Run program
main()

Hi there. The code suggested by scuttstorm goes good. I found another script made by Riyuzakisan, " mousemove 2.7 " that comes with an example tested from 2.66 to 2.70 versions.
You can find it here:

http://riyuzakisan.weebly.com/mousemove-script.html

This is a very nice simple setup. Unfortunately, it seems that there is a bug with setting mouse position to 0.5.
It sets it at .49915966, so the camera keeps moving very slowly

I tested the code pointed by thelaurent, and made a little change.

Actual wrong code:

from bge import logic as G
from bge import events
sensitivity = 0.8
speed = 0.1
owner = G.getCurrentController() .owner
# adjust the values below to edit the sensitivity
x = 0.5 - G.mouse.position[0]
y = 0.5 - G.mouse.position[1]
owner.applyRotation([ 0, 0, x], False)
#you can put a - infront of the x to reverse the mouse direction
owner.applyRotation([ -y, 0, 0], True)
#you can put a - infron of the y to reverse the mouse direction
G.mouse.position = (0.5,0.5)

Revised code:

from bge import logic as G
from bge import events
sensitivity = 0.8
speed = 0.1
owner = G.getCurrentController() .owner
# adjust the values below to edit the sensitivity
x = 0.5 - G.mouse.position[0]
y = 0.5 - G.mouse.position[1]


if abs(x) &gt; 0.01 : owner.applyRotation([ 0, 0, x], False)
#you can put a - infront of the x to reverse the mouse direction
if abs(y) &gt; 0.01 : owner.applyRotation([ -y, 0, 0], True)
#you can put a - infron of the y to reverse the mouse direction
G.mouse.position = (0.5,0.5)

Now it works ok. So we just need to add movements to parent.

ad0mas, my best suggestion to you is to take a look at Tutorials for Blender 3d (tutorialsforblender3d.com) and spend some time reading. It is seriously the best organized site I’ve seen for looking up BGE Python. Once you get going with Python for Blender you’ll find that it becomes very intuitive.

Thanks for fixing my script! Tested it, words much better now! I love this forum haha, hope that helps ad0mas

Thanks for fixing my script! Tested it, words much better now! I love this forum haha, hope that helps ad0mas

Sometimes the best codes are the shortest ones. I love it !!

Wow thanks for the replies guys :smiley: I got it to work!
The tutorial site is awesome but it looks like it’s mostly for Blender 2.4> but I’ll try to figure things out!

I tried the code written by scuttistorm but I have only up/down working. Left/Right doesn’t seem to respond at all… And another slightly off-topic question- when I run the embedded/standalone player all the textures and colours seem to be gone. Is it possible to make the game look as if it was rendered with full textures, lighting, shading etc? I tried playing around with texture modes (Singletexture/Multitexture/GLSL) but no luck :frowning:

Yeah. I think I may have modified that code to not accept LeftRight unless you turn it on in there somewhere. The assumption being you’d use keys to move left and right.

For running the stand-alone game you need to go to File->External Data->Pack into .blend. Otherwise when you compile the game it continues to point to those files rather than compile them as well.

Also, TutorialsForBlender3d has both 2.4 and 2.6 on the main page.

Unfortunately riyuzakisan’s mouselook script has errors in bge 2.79b. Also his code, website, and latest version is no where to be seen. I’m going to try scuttstorm’s idea and hopefully it’ll work, lol. Thanks for keeping it real.

4 years later, we have logic bricks

1 Like

IamKraZ download UPBGE (it fixes the mouse bugs), then just connect a mouse movement sensor to a mouselook actuator.
parent the camera to an object with this logic to create a third person view.

Great idea, but I’m working in blender original. Don’t get me wrong. I love UPBGE, but I’m wanting to find ways to make the regular BGE work. In 2.8 they are planning an overhaul of BGE. Plus finding ways around buggy software is what programmers (back in my day) actually did. I do have UPBGE and play around with it, but as I said I am trying to do this with the original.

Parent camera to a cube(player box).

player box:
always(true) -> and -> mouse(look) disable y axis

now select player box hold shift select camera.
add a mouse actuator on the camera disable x axis
connect the and with the mouse actuator, done!

Or simply search the resource section, tons of scripts that do work.

1 Like