Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Advanced Joystick Aim

DanikanamaDanikanama Member
edited June 2020 in Programming

***edit: SOLVED !**

hello, im working on my first game right now, for mobile .

i want the right joystick to be the aim, and when you pull it to its limits ,you trigger the shot action .

i know the joystick works on -1 +1 values for x and y .

(i use an asset store joystick https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631)

but i dont understand how to calculate the bounds of the joystick ,

ive tried to do it like this , the shootzone is set to 0.8f but i get dead zones where it should work but the x and y values are too low , north east west and south directions are fine , you can tell by the aim assist line color (red is shooting, white is not)

and here you can see the stick is in "shooting" position but the values dont get to 0.8 because my math is wrong and i dont know how to fix it :)

so in other words NE NW SE & SW directions are the problem .


Best Answers

  • MouledouxMouledoux Member
    Accepted Answer

    Have you considered using a different method for detecting joystick 'overextend'?

    Like instead of all these 'if' statements, why not just check the vertical and horizontal as a vector2, and compare it's magnitude to the 'dead zone'

    Vector2 stickPos = new Vector2(horizontal, vertical);
    if(stickPos.magnitude > aimJoystickShootZone)
    {
        Shooting();
    }
    

    this would check for all possible directions at once, all it does is tell you if you've move more than [some distance] from the center of the joystick.

  • MouledouxMouledoux Member
    Accepted Answer

    Glad it worked. The magnitude of a vector is its length, so it's not exactly how far its moved, but how far it is from the center.

Answers

  • JIMMY_VASHI04JIMMY_VASHI04 Member
    edited June 2020

    The if statement you showed is just fine so may be you can show us whats is in the shoot function.

    May be there you are lacking some calculations.

  • the thing the shoot method is not responsible to the aim , the if statement does it , here is the Shoot() method :


  • WarpWarp Administrator

    Place a log statement in the Shooting() function that just says "Shot Bullet".

    If this log appears, then it is a problem with your timestamp, or shooting code.

  • JIMMY_VASHI04JIMMY_VASHI04 Member
    edited June 2020

    Try to use && insted of || in the if statement

        //this is for NE direction Check
        if (joystick.Horizontal >= zone && joystick.Vertical >= zone) {
          //do Something
        }
        //this is for SE direction Check
        if (joystick.Horizontal >= zone && joystick.Vertical <= -zone) {
          //do Something
        }
        //this is for SW direction Check
        if (joystick.Horizontal <= -zone && joystick.Vertical <= -zone) {
          //do Something
        }
        //this is for NW direction Check
        if (joystick.Horizontal >= zone && joystick.Vertical <= -zone) {
          //do Something
        }
    
  • the solution with the Vector2 stickPos is exactly what i was looking for !!!! thanks man , it works great !

    .magnitude is calculating the distance that the vector has moved ?

Sign In or Register to comment.