Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Dual Analog Joystick - Check for joystick rotation?

Discussion in 'Scripting' started by Sydious, Mar 24, 2015.

  1. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I am looking for a way to check for Input from a game pad with dual analog joysticks. I have the game pad mapped and working just fine. What i need now is to figure out how one would go about checking for a clockwise or counter-clockwise rotation of the joystick. Like a "reeling a line in motion" or doing a large circle with the joystick. Can anyone explain to me how to do that or point me at something that may?
     
  2. JustJeff

    JustJeff

    Joined:
    Feb 16, 2015
    Posts:
    84
    One way would be to check quadrants of the axes (say that aloud... it sounds so smart ;) )

    Remember from algebra/geometry the 2D cartesian coordinate plane and its 4 quadrants:
    v--- x negative, y positive
    ______|_____x positive, y positive
    ........... | x positive, y negative
    ^---- x negative, y negative


    So, you can tell based on which axis changed sign (+ to -, or - to +)

    counter clockwise is (+,+) -> (-,+) -> (-,-) -> (+,-)

    This can be implemented and achieved in a variety of ways, the most intuitive of which would be to keep track of what the last quadrant was and start "reeling" when it's different than the last one and you'll be able to tell whether they're going clockwise or counter clockwise based on that pattern. Keep track of how much time since it changed last too and you should have a robust reeling system :)

    Good luck... your avatar is awesome BTW. It's like medieval/viking lore meets star wars which is amazing to me lolol
     
    Last edited: Mar 24, 2015
  3. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I understand your quadrant approach so thanks for that. I think it's the keeping track code I still haven't got my head around yet,
     
  4. JustJeff

    JustJeff

    Joined:
    Feb 16, 2015
    Posts:
    84
    Enum QuadrantTypes topright, topleft, bottomleft, bottomright

    in update:
    currQuadrant = determineQuadrant(xaxis, yaxis)

    if (currQuadrant != lastQuadrant)
    // Determine which way it's going in a number of ways, either with many
    // if statements or determining if the current quadrant is less than or greater
    // to the last quadrant if you want to implement something like that

    ---Make this the last thing you do in update---
    lastQuadrant = currQuadrant


    function that returns a quadrant type determineQuadrant(axis, yaxis)
    if (x>0 && y > 0)
    ---return QuadrantTypes.topright
    if (x<0 && y > 0)
    ---return QuadrantTypes.topleft

    etc.



    Enums are basically mapped to integers so when currQuadrant is greater than lastQuadrant, you're going counter clockwise based on the order of the quadrants in the enum above... just add some wraparound code so if currQuadrant is like 3 less than last quadrant, it's still going around the circle
     
  5. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    Awesome. I think I can work through this now. Thank you very much for your help!
     
  6. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    Oh and p.s. Thanks for the avatar comment. Wish I could take credit for the art but it came off the web and is not my own creation. I love it to! ;)
     
  7. JustJeff

    JustJeff

    Joined:
    Feb 16, 2015
    Posts:
    84
    You're quite welcome for the help and compliment. Good luck implementing this and with your game in general! :)