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

Help with mobile joystick C#

Discussion in 'Scripting' started by KaidaStudios, Aug 4, 2017.

  1. KaidaStudios

    KaidaStudios

    Joined:
    Aug 4, 2017
    Posts:
    34
    So my controller works perfectly on the computer when dragged by a mouse, however, Im getting complaints that when on mobile the controls are buggy and not playable at all.

    This is a video of our PC playing the game:


    and here is the code I made to find out where the analog stick was.. my only solution would be to turn the value into Screen.Width / something but Im somewhere lost as to how I can do this.

    Code (CSharp):
    1.     [SerializeField]private bl_Joystick Joystick;//Joystick reference for assign in inspector
    2.     [SerializeField]public bl_Joystick Joystick2;//Joystick reference for assign in inspector
    3.     public Transform camera;
    4.     public Animator anim;
    5.     public float v;
    6.     public float h;
    7.     public float h2;
    8.     [SerializeField]private float Speed = 5;
    9.  
    10.  
    11.     void Update()
    12.     {
    13.         //Step #2
    14.  
    15.         //Change Input.GetAxis (or the input that you using) to Joystick.Vertical or Joystick.Horizontal
    16.        v = Joystick.Vertical; //get the vertical value of joystick
    17.        h = Joystick.Horizontal;//get the horizontal value of joystick
    18.        h2 = Joystick2.Horizontal;//get the horizontal value of joystick
    19.         //in case you using keys instead of axis (due keys are bool and not float) you can do this:
    20.         //bool isKeyPressed = (Joystick.Horizontal > 0) ? true : false;
    21.  
    22.         //ready!, you not need more.
    23.  
    24.         if (v >= 0.1f && v <= 1.4f) {
    25.             anim.SetBool ("walk", true);
    26.             anim.SetBool ("walkb", false);
    27.         }
    28.  
    29.         if (v >= 1.5f) {
    30.             anim.SetBool ("run", true);
    31.         }
    32.            
    33.         if (v <= -0.9f) {
    34.             anim.SetBool ("walkb", true);
    35.             anim.SetBool ("walk", false);
    36.             anim.SetBool ("run", false);
    37.         }
    38.  
    39.         if (v == 0) {
    40.             anim.SetBool ("walk", false);
    41.             anim.SetBool ("walkb", false);
    42.             anim.SetBool ("run", false);
    43.         }
    44.  
    45.         if (h2 >= 0.4f) {
    46.             camera.Rotate (0, h2 * Time.deltaTime * 13, 0);
    47.         }
    48.  
    49.         if (h2 <= -0.4f) {
    50.             camera.Rotate (0, h2 * Time.deltaTime * 13, 0);
    51.         }
    52.     }
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Uh, what's the overall scale look like for v? Walk is between 0.1 and 1.4, and run is greater than 1.5? What's the maximum value?

    Most input systems (including Unity's) I've seen use normalized values (0 to 1), then scale accordingly.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    If your controls are misbehaving only on mobile, it could be that the bl_Joystick objects have not been set up properly to handle multiple touches, or as @BlackPete pointed out above, perhaps it isn't normalizing properly.

    Also, there may be "shunts" in the joystick code that directly map mouse motions when you are on a PC.Look for conditions based on Application.platform, for instance.
     
  4. unity_3A08355BB6CB65B34C79

    unity_3A08355BB6CB65B34C79

    Joined:
    May 29, 2021
    Posts:
    1
    I have got only one error in v = Joystick.Vertical;
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Please don't hijack old threads. It's against forum rules and you won't get any useful help.

    Instead, start your own post... it's FREE!

    When you post, how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/