Gamepad thumbstick to replace mouse aim

0 favourites
From the Asset Store
Total customisation of the input! You can combine inputs from all peripherals. Make your game accessible for everyone!
  • For my top down game (twin stick shooter with left stick movement, right stick aiming) I'm trying to replace the mouse with the right thumbstick but can't replicate the following:

    On Event -->

    Player: Set angle toward (Mouse.X(0), Mouse.Y(0))

    On Start Layout -->

    Mouse: Set cursor from sprite <Target reticule>

    So the left stick controls the usual WASD movement, and the right stick needs to move the cursor around the screen so that the player angle follows the cursor (rather than rotate the player directly). So I need a way of mapping the gamepad axis to an X,Y position for the cursor.

    Linked CAPX is based on the example in this thread:

    scirra.com/forum/360-aiming-with-a-gamepad-using-the-right-stick_topic73154.html

    CAPX: sendspace.com/file/52lruh

    Any suggestions appreciated.

  • So I need a way of mapping the gamepad axis to an X,Y position for the cursor.

    I'm not sure why you'd want this instead of the traditional twinstick style..

    but anyway, I dont have a gamepad to test this with, but to my knowledge gamepad.axis() returns a value inbetween -1 and 1

    so if you can correctly retrieve the width and height of the screen you can try this:

    set TargetX to (Width*0.5) + (Gamepad.Axis(0,2)*width*0.5)

    set TargetY to (Height*0.5) + (Gamepad.Axis(0,3)*height*0.5)

    In place of Width/Height You could try using WindowWidth and WindowHeight, but to my experience they can get a little messed up depending on other settings. I work around it by just creating global variables called BaseWidth and BaseHeight, I set them to the same setting as what I have for 'window size' in the project properties.

  • That makes sense but doesn't seem to work as it should, using Window Width/Height, global variables or hardcoded values. I seem to get huge X and Y values when the right stick is moved, so the cursor immediately goes off screen.

    Updated CAPX: sendspace.com/file/uljgcd

  • Why not just create a target reticule sprite and move it using the stick

    and instead of setting angle towards mouse.x,mouse.y set angle towards the sprite?

  • terence

    my bad, i just checked the manual and find that gamepad.axis() returns a value between -100 and 100, not -1 and 1 like I assumed.. so of course you just need to divide it

  • LittleStain, that's my goal. The mouse.x/mouse/y movement is what I'm trying to replicate.

    keepee, dividing by 100 works in cases where my layout matches the window size, but I'm facing a few issues:

    1. If my layout is much larger than the window size, the target stays within the top left of the layout and doesn't follow the player.

    2. When the right stick is not being used the target defaults to a position just off center of the window and the player sprite snaps back to a 0 angle. I'd like the target to stay where the it was last left (just like a mouse cursor) and have the player stay pointing towards that angle.

    3. The player angle isn't exactly aligned with the target. To set the target angle I'm using Set Angle To: angle(0,0,Gamepad.Axis(0,2),Gamepad.Axis(0,3))

    Any suggestions on how to address these issues?

  • ah I see, i misunderstood what you were after.

    to solve the first problem would have just been a case of using 'scroll'

    set TargetX to scrollx + (Gamepad.Axis(0,2)*width*0.5)

    but that's not really relevant now I understand what you want..

    if you create some extra vars you can try this:

    add Gamepad.Axis(0,2)*sensitivity to OffsetX

    add Gamepad.Axis(0,3)*sensitivity to OffsetY

    set TargetX to OffsetX + ScrollX

    set TargetY to OffsetY + ScrollY

    you can use clamp() to limit the Offset inbetween the view edges using the ViewportLeft/Right/Top/Bottom expressions.

    if you wish to implement layout scaling or layout rotation to your game, you should create a UI layer with that does not scroll, scale or rotate, then there's no need to add on the ScrollX or ScrollY.

    If you do that though, you then need to determine what the TargetX is on the actual gameplay layer, so you may need to use the CanvasToLayer() expression

  • keepee, thanks that addresses points 1 and 3. Any suggestions on point 2? I can't think of any way to get the target to stay in position (and therefore the player angle) when the right stick is released.

  • did you make sure the Offset vars are global/static?

    if not, they will be returning to their initial value (probably 0) every tick

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yes, have set them to global variables. The target reticule still moves back towards the center the moment the right stick is released, as I guess the global variables are set to 0 as soon as this happens?

    When I try to set the player angle toward (Target.TargetX, Target.TargetY) after setting TargetX to OffsetX and TargetY to OffsetY every tick, the player doesn't rotate towards the reticule. The only way I can get this to work is if I use Set Angle To: angle(0,0,Gamepad.Axis(0,2),Gamepad.Axis(0,3)) but this snaps the player angle back to 0 on releasing the right stick.

    Not using ScrollX/Y as UI is on a separate layer.

  • remind me this - ill show u a picture of mine when i get home. someone else helped me with mine.

  • This is what I am trying to do as well

  • sxespanky do you have this screenshot or capx for this fix?

  • robertjs3, you may find the attached CAPX useful. It basically simulates hotline miami style gamepad controls

    https://www.sendspace.com/file/wmb8uh

  • stricky that is exactly what I am looking to do! Thanks stricky, I will check it out the capx when I can access it later.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)