Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Blender Game Engine 2.55 Simple Camera Look
- #Created by Mike Pan: mikepan.com
- # Use mouse to look around
- # W,A,S,D key to walk around
- # E and C key to ascend and decend
- from bge import logic as G
- from bge import render as R
- from bge import events
- from mathutils import *
- scene = G.getCurrentScene()
- yaw = scene.objects["Empty"]
- speed = 0.08 # walk speed
- sensitivity = 0.002 # mouse sensitivity
- smooth = 0.7 # mouse smoothing (0.0 - 0.99)
- cont = G.getCurrentController()
- owner = cont.owner
- Mouse = cont.sensors["Mouse"]
- w = R.getWindowWidth()//2
- h = R.getWindowHeight()//2
- screen_center = (w, h)
- # center mouse on first frame, create temp variables
- if "oldX" not in owner:
- #R.setMousePosition(w , h )
- R.setMousePosition(w + 1, h + 1)
- owner["oldX"] = 0.0
- owner["oldY"] = 0.0
- else:
- scrc = Vector(screen_center)
- mpos = Vector(Mouse.position)
- x = scrc.x-mpos.x
- y = scrc.y-mpos.y
- # Smooth movement
- owner['oldX'] = (owner['oldX']*smooth + x*(1.0-smooth))
- owner['oldY'] = (owner['oldY']*smooth + y*(1.0-smooth))
- x = owner['oldX']* sensitivity
- y = owner['oldY']* sensitivity
- rotation_y = yaw.localOrientation
- rotation_y = rotation_y[0]
- rotation_y = rotation_y[1]
- if rotation_y < -0.89:
- if y > 0:
- y = 0
- else:
- y = y
- elif rotation_y > 0.86:
- if y < 0:
- y = 0
- else:
- y = y
- # set the values
- owner.applyRotation([0, 0, x], False)
- yaw.applyRotation([y, 0, 0], True)
- # Center mouse in game window
- R.setMousePosition(*screen_center)
- keyboard = G.keyboard.events
- # KEY BINDINGS
- kbleft = events.AKEY # Replace these with others, if you wish
- kbright = events.DKEY # An example would be 'events.WKEY, DKEY, SKEY, and AKEY
- kbup = events.WKEY
- kbdown = events.SKEY #
- ##################
- if not 'moveinit' in owner:
- owner['moveinit'] = 1
- owner['mx'] = 0.0
- owner['my'] = 0.0
- #
- #
- #
- # ~~SETTINGS~~ #
- #
- #
- #
- # Tweak these settings to fit your needs
- owner['accel'] = 2.0 # Acceleration
- owner['maxspd'] = 1.0 # Top speed
- owner['friction'] = 0.75 # Friction percentage; set to 0.0 for immediate stop
- owner['movelocal'] = 1 # Move on local axis?
- if keyboard[kbleft]:
- owner['mx'] += owner['accel']
- elif keyboard[kbright]:
- owner['mx'] -= owner['accel']
- else:
- owner['mx'] *= owner['friction']
- if keyboard[kbup]:
- owner['my'] += owner['accel']
- elif keyboard[kbdown]:
- owner['my'] -= owner['accel']
- else:
- owner['my'] *= owner['friction']
- # Clamping
- if owner['mx'] > owner['maxspd']:
- owner['mx'] = owner['maxspd']
- elif owner['mx'] < -owner['maxspd']:
- owner['mx'] = -owner['maxspd']
- if owner['my'] > owner['maxspd']:
- owner['my'] = owner['maxspd']
- elif owner['my'] < -owner['maxspd']:
- owner['my'] = -owner['maxspd']
- # Actual movement
- owner.setLinearVelocity([owner['my'], owner['mx'], owner.getLinearVelocity()[2]], owner['movelocal'])
Advertisement
Add Comment
Please, Sign In to add comment