Joystick direction bugging out?

So I’m not sure why this is happening but every time I press the analog in the left direction the player goes left, which is fine. But the problem occurs when I move the stick towards the right because it still goes left even though i haven’t told it to. Is there something I’m missing?


joys = cont.sensors["joys"]
limit = 32768.0
jx = joys.axisValues[0]/limit
player["Dir"] = ""
spd = .065
cvrSpd = spd - .025
sprSpd= spd + .08
deadzone = .5   

if abs(jx) >= deadzone:
        player["Dir"] = "Left"  
        player.applyMovement((-cvrSpd, 0, 0), True)
            
    else:
        player["Dir"] = "None" 
        player.applyMovement((0, 0, 0), True)

???

EDIT: forgot 3 lines of code joy,limit & jx

Sorry I’m kind of a beginner, is there something I need to add to the script?

Sorry I’m just a beginner, was there something I just needed to add to the script or have i done it completely wrong?


Yes, the Python controller is the right place.

Sorry maybe I’m not being clear. I want the player to move left and right depending on the direction the stick is pointed. At the moment it only moves right even when I point the joystick to the left.

This is what I have currently in the script. The script has multiple functions I call when needed. This is the function not working to my liking.


joys = cont.sensors["joys"]
limit = 32768.0
jx = joys.axisValues[0]/limit
spd = .065
cvrSpd = spd - .025
sprSpd= spd + .08
deadzone = .5  

def coverState():
    
player["jxValues"] = jx
track.object = "anchor"
track.time = 3
anchor.setParent(anchorEmpty,0,1)
    
    if abs(jx) >= deadzone:
        player["Dir"] = "Right"  
        player.applyMovement((cvrSpd, 0, 0), True)  
         
    elif abs(jx) <= -deadzone:
        player["Dir"] = "Left"  
        player.applyMovement((-cvrSpd, 0, 0), True)
        
    else:
        player["Dir"] = "None" 
        player.applyMovement((0, 0, 0), True)

coverState():

My problem is that it moves towards the right not matter the direction I press on the joystick. My question is why does it not go left when I tell it to.

You know that abs() will never become negative?

If you want to treat values below 50% as “centered” you better process it a bit different:


NEGATIVE= -1
CENTERED = 0
POSITIVE = 1

...
def retrieveJoystickAxisDirection(joystickSensor, axis, deadzoneRatio):
    value = joystickSensor.axisValues[0] / 32768.0
    
    if abs(value) <= deadzoneRatio:
        return CENTERED
    
    return POSITIVE if value > 0 else NEGATIVE

Then you can call it that way:


X_AXIS = 0
DEADZONE_RATIO = 0.5
...
xDirection = retrieveJoystickAxisDirection(joystickSensor, X_AXIS , DEADZONE_RATIO)

if xDirection == POSITIVE:
   ...
elif xDirection == NEGATIVE:
   ...
else:
   ...

(untested)

I hope you get the idea

Oh no I didn’t know that, and it was abs that was messing it up xD, thanks. Also ill try out your method for the joystick to :smiley: