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. Dismiss Notice

Cannot get any other values than -1,0,1 with the joystick? (Sensitivity,Gravity,Dead)

Discussion in 'Editor & General Support' started by EwnT9, May 2, 2017.

  1. EwnT9

    EwnT9

    Joined:
    Apr 20, 2017
    Posts:
    31
    Hi,
    I cannot have any other values than -1,0,1 for the joystick. I changed the sensitivity, gravity and dead of the joystick Horizontal like this :
    jou.png
    I tried different settings for example using Key or Mouse Move instead of Joystick Axis, but nothing works.
    In my Update, I have this :
    Code (CSharp):
    1. Debug.Log(
    2.                 Input.GetAxisRaw("Horizontal") +
    3.                 " : " +
    4.                 Input.GetAxis("Horizontal")
    5.             );
    Any help?

    Thanks
     
  2. mihanocho

    mihanocho

    Joined:
    Mar 10, 2015
    Posts:
    296
    try (int)Input.GetAxisRaw("Horizontal") or Mathf.Round(...)
     
  3. EwnT9

    EwnT9

    Joined:
    Apr 20, 2017
    Posts:
    31
    No I want "other" values than -1,0,1, I don't want "int", I would like to have "float" like 0.34354.
     
  4. mihanocho

    mihanocho

    Joined:
    Mar 10, 2015
    Posts:
    296
    set sensitivity 3, dead 0.001, gravity 1-3
     
  5. EwnT9

    EwnT9

    Joined:
    Apr 20, 2017
    Posts:
    31
    Thanks, now when I go right with the joystick, it says 1. But when I stop pushing the joystick, it does not come back to 0 but to -0.01077548 (both getAxisRaw and getAxis).
     
  6. EwnT9

    EwnT9

    Joined:
    Apr 20, 2017
    Posts:
    31
    I found a way to make it work! (a little bit) as the -1 is in the middle of the joystick, but at least I have got float values! I had to press "Analog" on the joypad, and put sensitivity really low, like 0.01.

    I am trying to have the joystick being -1 only if the joystick is at the maximum on the left, now it is -1 at the middle... if you know a good way. (I am not sure about what settings to add in gravity/sensitivity).
     
    Last edited: May 4, 2017
  7. mihanocho

    mihanocho

    Joined:
    Mar 10, 2015
    Posts:
    296
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using System.Collections.Generic;
    5.  
    6.     public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    7.     {
    8.  
    9.     public static  Dictionary<Type, Joystick>  instance=new Dictionary<Type, Joystick>();
    10.     public enum Type{
    11.         Left,Right
    12.     }
    13.     public enum AxisOption
    14.         {
    15.             // Options for which axes to use
    16.             Both, // Use both
    17.             OnlyHorizontal, // Only horizontal
    18.             OnlyVertical // Only vertical
    19.         }
    20.         public Type type;
    21.         public int MovementRange = 100;
    22.         public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
    23.         public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
    24.         public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
    25.  
    26.         Vector3 m_StartPos;
    27.         bool m_UseX; // Toggle for using the x axis
    28.         bool m_UseY; // Toggle for using the Y axis
    29.         public float X;
    30.         public float Y;
    31.         //CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
    32.         //CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
    33.         void Awake()
    34.     {
    35.         if (instance.ContainsKey (type))
    36.             instance.Remove (type);  
    37.         instance.Add (type, this);
    38.     }
    39.         void Start() //DCURRY: Changed to Start from OnEnable
    40.         {
    41.             m_StartPos = transform.position;
    42.             CreateVirtualAxes();
    43.         }
    44.     public Vector2 GetAxis()
    45.     {
    46.         return new Vector2(X, Y);
    47.     }
    48.     void UpdateVirtualAxes(Vector3 value)
    49.         {
    50.             var delta = m_StartPos - value;
    51.             delta.y = -delta.y;
    52.             delta /= MovementRange;
    53.             if (m_UseX)
    54.             {
    55.                 X=-delta.x;
    56.             }
    57.  
    58.             if (m_UseY)
    59.             {
    60.                 Y=delta.y;
    61.             }
    62.         if (Mathf.Abs(X) > 0 || Mathf.Abs(Y) > 0)
    63.         {
    64.             Vector2 screenSize = new Vector2(Screen.width/2 , Screen.height /2);
    65.             Vector2 directScreenPos = Vector3.zero;
    66.             if (X < 0)
    67.             {
    68.                 directScreenPos.x = screenSize.x * (1 + X);
    69.             }
    70.             else if (X > 0)
    71.             {
    72.                 directScreenPos.x = screenSize.x + screenSize.x * X;
    73.             }
    74.             if (Y < 0)
    75.             {
    76.                 directScreenPos.y = screenSize.y * (1 + Y);
    77.             }
    78.             else if (Y > 0)
    79.             {
    80.                 directScreenPos.y = screenSize.y + screenSize.y * Y;
    81.             }
    82.             ControlDirectionMove.UpdateDirection(directScreenPos);
    83.         }
    84.     }
    85.  
    86.  
    87.  
    88.         public void OnDrag(PointerEventData data)
    89.         {
    90.             Vector3 newPos = Vector3.zero;
    91.  
    92.             if (m_UseX)
    93.             {
    94.                 int delta = (int)(data.position.x - m_StartPos.x);
    95.                 //delta = Mathf.Clamp(delta, - MovementRange, MovementRange); //DCURRY: Dont want to clamp individual axis
    96.                 newPos.x = delta;
    97.             }
    98.  
    99.             if (m_UseY)
    100.             {
    101.                 int delta = (int)(data.position.y - m_StartPos.y);
    102.                 //delta = Mathf.Clamp(delta, -MovementRange, MovementRange); //DCURRY: Dont want to clamp individual axis
    103.                 newPos.y = delta;
    104.             }
    105.             //DCURRY: ClampMagnitude to clamp in a circle instead of a square
    106.             transform.position = Vector3.ClampMagnitude(new Vector3(newPos.x, newPos.y, newPos.z), MovementRange) + m_StartPos;
    107.             UpdateVirtualAxes(transform.position);
    108.         }
    109.  
    110.  
    111.         public void OnPointerUp(PointerEventData data)
    112.         {
    113.             transform.position = m_StartPos;
    114.             UpdateVirtualAxes(m_StartPos);
    115.         }
    116.  
    117.  
    118.         public void OnPointerDown(PointerEventData data) { }
    119.  
    120.        
    121.     }
     
  8. mihanocho

    mihanocho

    Joined:
    Mar 10, 2015
    Posts:
    296
    To use from anywhere:
    Code (CSharp):
    1. float X = Joystick.instance [Type.Left].X;
    2.         float Y = Joystick.instance [Type.Left].Y;
     
  9. EwnT9

    EwnT9

    Joined:
    Apr 20, 2017
    Posts:
    31
    What does this do? I put the Type enum outside of the Joystick class (otherwise Type is not defined in my Test.cs), but then I have got this :
    for the line with : float X ...
     
  10. mihanocho

    mihanocho

    Joined:
    Mar 10, 2015
    Posts:
    296
    Sorry Joystick.instance[Joystick.Type.Left].X.