UPBGE Joystick

Hello everyone. I am working on a game using UPBGE and would like to be able to use a USB gamepad to control it. I have the character’s motion figured out, but I can’t figure out how to get the character to face in the direction in which the left thumbstick is pointed. I have a simple cube as the hitbox (it is the object that moves) and a cone parented to it. I want the cone to be the only thing that faces in the direction of the joystick. I appreciate any help.

Edit Object actuator set to Track To; have your cone track to an empty parented to the hitbox.
When the empty moves, the cone will automatically rotate however much it needs to in order to face it’s target. So all we need to do is multiply a fixed distance by x and y values of the left stick.

from bge.logic import joysticks, getCurrentController
own = getCurrentController().owner
target = own.scene.objects["Empty"]

joy = joysticks[1]
x, y = joy.axisValues[0:2]

if x or y:
    target.localPosition.xy = [2*x, 2*y]

This would be the most basic approach; your usecase may need additional polish.

Thanks for the quick response. I will try what you have suggested.

I tried adding the script in and got it to work. Would it be possible to make the target empty stay a certain distance from the player cube?

The number you multiply times x and y (2 in the example script) represents maximum distance along an axis. So with this script, minimum distance depends on thumbstick dead zone.

In other words, max distance * deactivation treshold = min distance; the bigger maximum, the bigger minimum. That’s a quick fix.

A more refined solution would be something like a limit distance constraint, but I’m not 100% sure how to do it.

Ok. Thanks for the help.

Could you show your setup? I’m not sure I understand from the text, and I’m having the same problem.

This is my setup.