character rotation with mouse movement

i need a script to rotate my object character no need for camera rotation no need for mouse look scripts with camera

to rotate my character when he walk with mouse movement i use this old script i believe is not really perfect since it’s using mouse look script with the scripts part of camera rotation deleted from it.

# define main program
def main():
    
    # set default values
    Sensitivity =  0.001
    Invert = 1
    Capped = False
    
    import bge    
    
    # 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():
    
    import bge
    
    # 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):
    
    import bge


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


    # extract width and height from gameScreen
    width = bge.render.getWindowWidth()
    height = bge.render.getWindowHeight()


    # 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 obj.get('mouseInit') == False:
        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 obj.get('Adjust') is not None:
        
        # Don't want Negative values
        if obj['Adjust'] < 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 obj.get('Invert') == True:
            
        # 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 obj.get('Cap') is not None:
    
        # import mathutils
        import mathutils
        
        # get the orientation of the camera to world axis
        camOrient = obj.orientation
        
        # get camera Z axis vector
        camZ = [camOrient[0][2], camOrient[1][2], camOrient[2][2]]
        
        # create camera z axis vector 
        vec1 = mathutils.Vector(camZ)
        
        # get camera parent
        camParent = obj.parent
        
        # get parent orientation to world axis
        parentOrient = camParent.orientation
        
        # get parent z axis vector
        parentZ = [parentOrient[0][2], parentOrient[1][2], parentOrient[2][2]]
        
        # create parent z axis vector
        vec2 = mathutils.Vector(parentZ)
        
        # find angle between two in radians
        angleTmp = vec1.angle(vec2)
        
        import math
        
        # convert angle to degrees
        angle = math.degrees(angleTmp)-90
                    
        # get amount to limit mouselook
        capAngle = obj['Cap']
        
        # limit cap to maximum of +/-85 degrees
        if capAngle > 85:
            capAngle = 85
        if capAngle < -85:
            capAngle = -85
        
        # get mouse up down movement
        moveY = move[1] * invert
        
        # check capped angle against against camera z-axis and mouse y movement
        if (angle < -capAngle and moveY < 0) or (angle > capAngle and moveY > 0):
                        
            # no movement
            capped = True
    
    # return capped
    return capped


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


# define useMouseLook
def useMouseLook(controller, capped, move, invert, sensitivity):
                
    # get left/right movement
    leftRight = move[0] * sensitivity * invert 
        
    # Get the actuators
    act_LeftRight = controller.actuators["LeftRight"] 
    
    # set the values
    act_LeftRight.dRot = [ 0.0, 0.0, leftRight]
    act_LeftRight.useLocalDRot = False  


    
    # Use the actuators 
    controller.activate(act_LeftRight)
    
#############################################


# define center mouse cursor
def centerCursor(controller, gameScreen):
    
    import bge
    
    # extract width and height from gameScreen
    width = bge.render.getWindowWidth()
    height = bge.render.getWindowHeight()
    
    # 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"] 
        
        # turn off the actuators 
        controller.deactivate(act_LeftRight)


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


# Run program
main()

any one have a good script to rotate the character with mouse movement no need for camera rotation like in in this attachment:

Attachments

rot_test.blend (429 KB)

mouse look actuator

thank you but i want a python script

look into my sig at the movement scripts ,examples can be found there. Or search in resource section of the forum, plenty of them uses python to rotate.

Also:
import bge have to be called once, not in every function, just put it at the top of your script
also i see your using bricks to rotate now, you can replace that part of code with applyRotation()

thanks, i need only script to rotate object no need for camera, do you have?

thanks, but all what i needed is as simple as a few lines script that simply rotate an object with mouse movement, but i keep find scripts with camera rotation involved or complicated script with many lines. until i found a tutorial in youtube, this is simple and few lines script that simply rotate an object with mouse movement:

import bgefrom bge import render
from mathutils import Vector




def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner


    mouse = cont.sensors['mouse']
    rotz = cont.actuators['rotz']


    cont.activate(rotz)


    x = render.getWindowWidth()//2
    y = render.getWindowHeight()//2


    screenCenter = (x,y)
    center = Vector(screenCenter)


    mousePosition = Vector(mouse.position)
    offset = (mousePosition - center) * -0.002


    rotz.dRot = [0, 0, offset.x]
    render.setMousePosition(x,y)


main()

Attachments

simplerot.blend (407 KB)

from bge import logic

owner = logic.getCurrentController().owner

speed = 4
ratio = 1080/1920

rawX, rawY = logic.mouse.position #(0.5, 0.5) is center

X = (rawX-0.5)*speed
Y = (rawY-0.5)*speed*ratio

owner.applyRotation((0.0, Y, X), True) #set to False for world space

please add .blend Attached File

make sure line 6 says “ratio”. i just fixed a typo.

make sure your in script mode connected by an “Always” sensor with True Pulse ON (3 little dots icon on left).

thank you very much