Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Joystick Input directions

Discussion in 'Getting Started' started by blazon_sa, Nov 23, 2018.

  1. blazon_sa

    blazon_sa

    Joined:
    Nov 23, 2018
    Posts:
    4
    Hello,

    I set up the Unity input axes and connected an Xbox 360 controller.
    Analog stick input works, but only in 8 directions.
    How can I get a smooth 360-degree orientation from the analog stick?

    Thank you.
     
  2. gamedevbill

    gamedevbill

    Joined:
    May 25, 2018
    Posts:
    43
    8 directions? Meaning the cardinals (+/-x, +/-y), then the diagonals as well? When I've used a joystick input, I've only ever looked at the cardinal directions, so I guess I missed that there might have been more input. Often that's all I've needed (some amount of x and y force), though if I need to know an angle orientation, I've just done the math (sin/cos/tan) to figure it out. You can read up on your trigonometry online, or utilize this class https://docs.unity3d.com/ScriptReference/Vector2.html
     
    blazon_sa likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,943
    Trigonometry.

    Code (csharp):
    1. float angle = Mathf.Atan2(vertical, horizontal) * Mathf.Rad2Deg;
     
    Last edited: Nov 25, 2018
  4. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    I'm a bit curious about what you're doing that requires an angle in degrees. All the code I've seen ends up using vectors, so a conversion like this doesn't have any use (in the code I've seen). So, like I said, I'm a bit curious what this is going to be used for.
     
  5. gamedevbill

    gamedevbill

    Joined:
    May 25, 2018
    Posts:
    43
    I can't actually answer this for @blazon_sa but I've needed the angle frequently for things I'm rotating. For example, in a twin-stick-shooter, one stick controls movement (x & y directions) the other where your gun is pointed (rotation of the stick).
     
  6. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    That's the part that confuses me a bit. If the gun is supposed to point in the same direction as the stick, wouldn't turning the sticks inputs into a Vector2 (or Vector3) and setting the forward direction of the gun to that vector be a easier? You might need to pass that vector through the camera in order to transform that rotation from Camera-Space to World-Space. I did quite a few of these types of rotations with dot-products and vector additions for a project.
     
  7. blazon_sa

    blazon_sa

    Joined:
    Nov 23, 2018
    Posts:
    4
    Thank you all for your suggestions and input; I'm finally looking into this today. :D
     
  8. blazon_sa

    blazon_sa

    Joined:
    Nov 23, 2018
    Posts:
    4
    I'm just rotating a character based on the joystick input. If I could pull an angle for the Y rotation of my character from my horizontal and vertical input axes, I'd be done.

    I'm using this, and didn't expect to have to do this manually -
    https://forum.unity.com/threads/released-complete-physics-platformer-kit.203279/

    What am I doing wrong? Why would someone make a third-person control system that didn't have gradual orientation between input axis poles? I'm so confused.
     
    Last edited: Dec 1, 2018
  9. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    I'm not familiar with the framework the link talks about, I do know a few ways to rotate things without using too much math. Most of the rotations I've done use the LookAt(...) method. You create a forward direction vector that's in World-Space, and you add that to the GameObject's position in World-Space. When you pass that to the LookAt(...) method, the object will rotate so that it's forward direction now faces the direction you want it to face. The LookAt(...) method is also overloaded so that you can pass in a Up direction, just in case the character's Up direction is different than the default Up direction. With this method of rotation, you can point your character straight at the direction the joystick is pointing at. Then it become a question of mapping the joystick to the game's World-Space.

    Code (CSharp):
    1. private void RoateCharacter01(float joystickXAxisValue, float joystickYAxisValue, Vector3 joystickUp, Vector3 joystickRight, Vector3 characterUp, bool invertYAxis)
    2. {
    3.     var xComp = joystickXAxisValue * joystickRight;
    4.     var zComp = (invertYAxis ? -1.0f : +1.0f) * -joystickYAxisValue * joystickUp;
    5.     var dirForward = xComp + zComp;
    6.     if (dirForward.sqrMagnitude < 0.00001f)
    7.         return;
    8.     dirForward.Normalize();
    9.     this.transform.LookAt(dirForward + this.transform.position, characterUp);
    10. }
    11.  
    In the code above, you pass in the values of the joystick. If those values are too small to be of any use, we exit. Besides passing in the values of the joystick, you also pass in two directions. You pass in the direction you want your character to face when the joystick is all the way to the right, and the direction you want the character to face when the joystick is all the way up. We multiply the joystick values by the direction values to create the components of the Forward Direction we want the character to face. We combine the components and normalize the result to create a Forward Direction vector, which we then pass to the LookAt(...) method to rotate the character. This is one way to do this that will have the character always facing in the direction that the joystick is pointing at. For a game like Geometry Wars, code like this should work fairly well.

    That being said, I don't know how well this will work with the asset package you are using and I'm not will put down $25 to find out. The fact that it hasn't been updated in over 3 years doesn't help either.
     
    blazon_sa likes this.
  10. insoluzioni

    insoluzioni

    Joined:
    Nov 25, 2018
    Posts:
    6
    I think he's using physics to apply rotation, such as AddTorque, instead of transform.
     
  11. plborgprograms

    plborgprograms

    Joined:
    Jul 13, 2020
    Posts:
    1