aiming problem!.

when I press the right mouse button to aim the player aim up,sometimes down but he should aim in the center first and when I move the mouse up down, he follow the movement.I think the problem is in the script. does anyone can tell me what should add in this script!.

import bge
GameLogic = bge.logic
from Rasterizer import *
    
cont = GameLogic.getCurrentController()
obj = cont.owner
Aim = cont.sensors["sAim"]
MouseMove = cont.sensors["sMouseMove"]
Sensitivity = 0.2 
CenterY = getWindowHeight()//2
CenterX = getWindowWidth()//2
    
if Aim.positive:
    YPosition = MouseMove.position[1]
    xRot = (CenterY - YPosition)*Sensitivity
    obj["AimUpDown"] -= xRot
    if obj["AimUpDown"] < -11:
        obj["AimUpDown"] = -11
    if obj["AimUpDown"] > 11:
        obj["AimUpDown"] = 11
    setMousePosition(CenterX,CenterY)
        
cont.activate("AimUpDown")

the .blend file ( origin made by HG1 ) :

please use code tags rather then quote tags

(you can edit your own posts)

Edit:

The first call to read the mouse position always returns [0,0]

You should start aiming with the second frame. You can do that with a delay sensor.

While you’re designing you should enable visibility of the mouse cursor in Properties > Render.

Since your mouse is unlikely to be centered when you first R-click, the arm aims some random direction.

If you put this in your script:

if MouseMove.positive:
    bge.render.setMousePosition(int(CenterX), int(CenterY))

just prior to the activation of the Aim sensor it will work as long as you have moved the mouse since starting the game. To avoid that potential glitch you could center the mouse at startup with a script attached to a non pulsing Always sensor.

many thanks@Equip!,I’ve put your line in the script and it works fine now!