Two questions.

Hello.

As you should know, I have two questions.

  1. Is there a way to limit the position of the mouse? I want it to only move within a certain area on the y axis, and not move at all on the x axis.

  2. How would you make a machine gun type thing? I know you could use rays, but how would I do it so that it sends out one ray every frame in a random direction? ( but still towards the crosshair)

Thank you all.

For the second one, you could send a ray from a rotating empty.

Okay ya, but then wouldn’t it always be the same pattern?

Edit: Nevermind. i’m gonna try it out and see how it works.

Nope. I don’t know what I’m doing.

Here is a blend. Machine Gun Example.blend (145 KB)

Ok thanks, but I don’t want it to fire in a pattern. I would like each shot to be in a completely random direction.

Here is my method:

Have an empty with an IPO rotation around X (a limited rotation).
Parent to the empty another empty with an IPO rotation around Z (a limited rotation as well).
Parent the ray emitter to this empty.

Both rotation emptys get an IPO actuator in property mode.
Let each property set by a random actuator.
The “pattern” depends on the granularity of your random values. Make sure there are no random values outside your IPO curves (see the extended modes in IPO editor)


I hope it helps.

Awesome. It works perfectly. Even though there is a pattern, it is not obvious in any way. Thanks a lot.

Now just the problem with the mouse.

Just put in a few lines of code that limit the position of the mouse.

Ok but I have no clue when it comes to Blender and Python together.

This is the mouselook code that I use:

######################################################
#
#    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, the only thing you have to set

# Get controller
controller = GameLogic.getCurrentController()

# Get sensor named Mouse
mouse = controller.getSensor("Mouse") #make a mouse: movement sensor and name it "Mouse"
   
# Get the actuators
rotLeftRight = controller.getActuator("LookLeftRight") #name this actuator "LookLeftRight"
rotUpDown = controller.getActuator("LookUpDown")  #name this actuator "LookUpDown"

#connect the Mouse sensor to the Python controller using this code
#and connect that to the two actuators. this can be used on a Camera by itself
#or LookLeftRight on one logic brick on a character, and LookUpDown on the camera.

############################## 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)

Ok but I’m not trying to get mouselook. I’m trying to limit the position of the cursor on the screen. I want it to not move at all on the x axis, and only move a certain distance on the y axis.

Basically, I have an object that is set where a ray from the mouse hits, and I don’t want that object to hit the top or bottom of the screen. I want to limit it to the center third or so.


# [min,max]
MOUSELIMITY = [10,100]
MOUSELIMITX = [20,100]
 
from Rasterizer import *
 
screenx = getWindowWidth()
screeny = getWindowHeight()
 
mouse = cont.sensors['Mouse']
mx = mouse.getXPosition()
my = mouse.getYPosition()
 
if screenx-mx > MOUSELIMITX[1]:
setMousePosition(MOUSELIMITX[1],my)
elif screeny-my > MOUSELIMITY[1]:
setMousePosition(mx,MOUSELIMITY[1])
elif mx < MOUSELIMITX[0]:
setMousePosition(MOUSELIMITX[0],my)
elif my < MOUSELIMITY[0]:
setMousePosition(mx,MOUSELIMITY[0])
 
showMouse(1)
 

I haven’t tested it…but thats how it should work. You only need to edit the first lines. There are instructions.

It says ‘name getXPosition is not defined’.

Oh sorry about that, you need a mouse movement sensor called Mouse. I also edited the script a bit.

Ok there we go. I got it now. So how is the screen measured? Like, what would I put in for the numbers for the far right, bottom etc. Or does it just go by screen resolution?

I honestly have no idea…it is measured in screen res, but it could be measured in anything if you really wanted to write a script like that.

Alrighty. Thanks for the script. It should work quite well. Just might take a bit to set up.

Cool. Just PM me if you need anything else. :slight_smile: