[Tutorial]How to use Blender Game Engine Mouse Follow

Moderators: Yodaman921, Gabriel

[Tutorial]How to use Blender Game Engine Mouse Follow

Postby Elec0 » Thu Jan 22, 2009 4:38 pm

Here is a tutorial on how to use the mouse following feature in BGE.






Here is the script that you will need:



Code: Select all
######################################################
#
#    MouseLook.py        Blender 2.46
#
#    Tutorial for using MouseLook.py can be found at
#
#    www.tutorialsforblender3D.com
#                                                                                                         
######################################################

# import Rasterizer
import Rasterizer

# get controller
controller = GameLogic.getCurrentController()

# get the object this script is attached to
player = controller.getOwner()

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

# get width and height of game window
width = Rasterizer.getWindowWidth()
height = Rasterizer.getWindowHeight()
     
# define mouse movement function
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(player, 'mouseInit') == False:
      x = 0
      y = 0
      # bug in Add Property
      # can't use True.  Have to use 1
      player.mouseInit = 1
      
   # return mouse movement
   return (x, y)

# get mouse movement from function
move = mouseMove()

# set mouse sensitivity
sensitivity = 0.001

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

# set the values
lookLeftRight.setDRot( 0.0, 0.0, leftRight, False)   
lookUpDown.setDRot( upDown, 0.0, 0.0, True)

# Use them
GameLogic.addActiveActuator(lookLeftRight, True)
GameLogic.addActiveActuator(lookUpDown, True)

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

Image
User avatar
Elec0
Member
Member
 
Posts: 210
Joined: Mon Jun 09, 2008 2:45 pm
Location: C:\Universe\Milky Way\Sol\Earth\North Americia\Tennessee

Re: [Tutorial]How to use Blender Game Engine Mouse Follow

Postby hydra_skillz » Thu Jan 22, 2009 9:29 pm

Nice tutorial, but might want to call it mouse look not mouse follow.
GE API <- reference for game engine scripting
User avatar
hydra_skillz
Premium Member
Premium Member
 
Posts: 1321
Joined: Mon Dec 15, 2008 5:22 pm
Location: North Carolina

Re: [Tutorial]How to use Blender Game Engine Mouse Follow

Postby Elec0 » Fri Jan 23, 2009 7:49 am

Ah well, either way.
Heh, I actually edited the script so it would be a mouse follow. :)
Image
User avatar
Elec0
Member
Member
 
Posts: 210
Joined: Mon Jun 09, 2008 2:45 pm
Location: C:\Universe\Milky Way\Sol\Earth\North Americia\Tennessee

Re: [Tutorial]How to use Blender Game Engine Mouse Follow

Postby renox777 » Sat Jan 24, 2009 3:47 pm

It wont let me paste it. Is there a key i can press, cuz i just use the rmb and it wont do it.
renox777
Member
Member
 
Posts: 38
Joined: Sat Nov 24, 2007 1:42 am

Re: [Tutorial]How to use Blender Game Engine Mouse Follow

Postby hydra_skillz » Sat Jan 24, 2009 5:38 pm

Press select all and then ctrl + c, then ctrl + v in the text window.
GE API <- reference for game engine scripting
User avatar
hydra_skillz
Premium Member
Premium Member
 
Posts: 1321
Joined: Mon Dec 15, 2008 5:22 pm
Location: North Carolina

Re: [Tutorial]How to use Blender Game Engine Mouse Follow

Postby 3D Joel » Sun May 03, 2009 1:17 pm

Hey, I'm looking for something close to that... but not quite the same.

I want to attach an empty to the movement of the camera... That way when I click, where ever my mouse it, that will be where the empty shoots the object.

I already know how to make emptys create object's and all, I just need to know how to have the empty follow the mouse...
One goal, one passion - 3D Joel
3D Joel
Member
Member
 
Posts: 272
Joined: Tue Oct 02, 2007 1:51 pm

Re: [Tutorial]How to use Blender Game Engine Mouse Follow

Postby chewanater » Sun May 03, 2009 5:41 pm

I followed the tutorial and it did nothing. What version of Python do I need? I have 2.5 (I've actually been trying to get 2.6 installed).
Last edited by chewanater on February 31st, 2011, 4:03 am, edited 1 time in total.

ImageImage
User avatar
chewanater
Premium Member
Premium Member
 
Posts: 1214
Joined: Fri Dec 19, 2008 10:19 am

Re: [Tutorial]How to use Blender Game Engine Mouse Follow

Postby hydra_skillz » Thu May 28, 2009 8:31 pm

You need 2.5. If there aren't any errors in the console window, then you probably connected a logic brick wrong.
GE API <- reference for game engine scripting
User avatar
hydra_skillz
Premium Member
Premium Member
 
Posts: 1321
Joined: Mon Dec 15, 2008 5:22 pm
Location: North Carolina

Re: [Tutorial]How to use Blender Game Engine Mouse Follow

Postby Samuel_benson » Fri Aug 14, 2009 10:14 am

WOW, didn't think it was that easy! Thanks for the tutorial really clear to follow, one question: how about the camera following the mouse, but its centre of rotation is around a vehicle which can be driven using the keyboard? (no zoom capabilities wanted) if you have played the game Halo...

Cheers in advance

Sam
Samuel_benson
Member
Member
 
Posts: 1
Joined: Fri Aug 14, 2009 10:06 am

Re: [Tutorial]How to use Blender Game Engine Mouse Follow

Postby billymcguffin » Fri Aug 14, 2009 11:01 am

this template has that feature (rotate around the character).
Image
My deviantART
The Rules. Read them.
Bleep Bloop Blop =B
User avatar
billymcguffin
Premium Member
Premium Member
 
Posts: 4301
Joined: Tue Dec 09, 2008 6:17 pm
Location: Minnesota, US

Next

Return to User Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron