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

Virtual joystick not working

Discussion in 'UGUI & TextMesh Pro' started by Myrmidou, Jul 13, 2016.

  1. Myrmidou

    Myrmidou

    Joined:
    Jun 7, 2014
    Posts:
    30
    Hi there!

    A few months ago I worked on a project with a working virtual joystick. Now I work on a 2.0 of this project, so I duplicated it, changed a few things and the joystick is not working anymore.

    First the joystick script:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using System.Linq;
    5.  
    6. namespace UnityStandardAssets.CrossPlatformInput
    7. {
    8.     public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    9.     {
    10.         public enum AxisOption
    11.         {
    12.             // Options for which axes to use
    13.             Both, // Use both
    14.             OnlyHorizontal, // Only horizontal
    15.             OnlyVertical // Only vertical
    16.         }
    17.  
    18.         public int MovementRange = 100;
    19.         public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
    20.         public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
    21.         public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
    22.  
    23.         Vector3 m_StartPos;
    24.         bool m_UseX; // Toggle for using the x axis
    25.         bool m_UseY; // Toggle for using the Y axis
    26.         CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
    27.         CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
    28.  
    29.         public GameObject[] Walls; //4 walls around the ground
    30.         public float[] WallDist; //Distance between wall & camera
    31.         public Camera DetachedCamera; //Active camera during detachement mode
    32.  
    33.         void OnEnable()
    34.         {
    35.             CreateVirtualAxes();
    36.         }
    37.  
    38.         void Start()
    39.         {
    40.             m_StartPos = transform.position;
    41.             //Debug.Log ("joystick start");
    42.         }
    43.  
    44.         void UpdateVirtualAxes(Vector3 value)
    45.         {
    46.            
    47.             var delta = m_StartPos - value;
    48.             delta.y = -delta.y;
    49.             delta /= MovementRange;
    50.             if (m_UseX)
    51.             {
    52.                
    53.                 m_HorizontalVirtualAxis.Update(-delta.x);
    54.                 //Debug.Log (m_HorizontalVirtualAxis.GetValueRaw);
    55.             }
    56.  
    57.             if (m_UseY)
    58.             {
    59.                 m_VerticalVirtualAxis.Update(delta.y);
    60.             }
    61.         }
    62.  
    63.         void CreateVirtualAxes()
    64.         {
    65.             // set axes to use
    66.             m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
    67.             m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
    68.  
    69.             // create new axes based on axes to use
    70.             if (m_UseX)
    71.             {
    72.                
    73.                 m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
    74.                 CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
    75.             }
    76.             if (m_UseY)
    77.             {
    78.                 m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
    79.                 CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
    80.             }
    81.         }
    82.  
    83.         public void OnDrag(PointerEventData data)
    84.         {
    85.             Vector3 newPos = Vector3.zero;
    86.  
    87.             if (m_UseX)
    88.             {
    89.                 int delta = (int)(data.position.x - m_StartPos.x);
    90.                 delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
    91.                 newPos.x = delta;
    92.             }
    93.  
    94.             if (m_UseY)
    95.             {
    96.                 int delta = (int)(data.position.y - m_StartPos.y);
    97.                 delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
    98.                 newPos.y = delta;
    99.             }
    100.             transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
    101.             UpdateVirtualAxes(transform.position);
    102.         }
    103.  
    104.         public void OnPointerUp(PointerEventData data)
    105.         {
    106.             transform.position = m_StartPos;
    107.             UpdateVirtualAxes(m_StartPos);
    108.         }
    From here everything is working fine. The virtual axis is returning a value and updated.

    I think the problem comes from here:

    (axis infos fetched L.301, infos used L.137)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using System;
    7. using UnityStandardAssets.CrossPlatformInput;
    8.  
    9. namespace ImmersiveLM {
    10.     /// <summary>
    11.     /// This manager handles all interactions possible in "Detached mode" (or 3DView).
    12.     /// </summary>
    13.     public class DetachedModeControls : MonoBehaviour {
    14.         #region Singleton
    15.         static private DetachedModeControls s_Instance;
    16.  
    17.         static public DetachedModeControls GetInstance {
    18.             get {
    19.                 return s_Instance;
    20.             }
    21.         }
    22.  
    23.         void Awake() {
    24.             if (s_Instance == null) {
    25.                 s_Instance = this;
    26.             }
    27.             DontDestroyOnLoad(this);
    28.         }
    29.         #endregion Singleton
    30.  
    31.         #region Members
    32.         #region Objects setable in Inspector
    33.         [Tooltip("Check this if you want the script to search for the needed gameobjects on start."
    34.             + "\ni.e. You won't have to add them manually in the inspector.")]
    35.         public bool m_ForceGameobjectsSearch = true;
    36.         public GameObject m_GroundForDetachedMode;
    37.         [Header("Detached mode's UI")]
    38.         public GameObject m_BathroomSizePanel;
    39.         public GameObject m_DetachedControlsPanel;
    40.  
    41.         public GameObject m_EstimateButton;
    42.         //public GameObject OD;
    43.  
    44.         public Slider m_ZoomSlider;
    45.         [Header("Miscelleanous")]
    46.         //public GameObject m_GroundPlacementTarget;
    47.         public Joystick m_RotationStick;
    48.         #endregion Objects setable in Inspector
    49.  
    50.         private BathroomSizeUI m_BathroomSizeUIScript;
    51.  
    52.         #region Constants & readonly
    53.         // Will be const after tweak.
    54.         public float c_HorizontalRotationSpeed = 1.0f;
    55.         public float c_VerticalRotationSpeed = 1.0f;
    56.  
    57.         // All distances are stored in meters.
    58.         public const float c_GroundTargetOffsetY = 0.0f;
    59.  
    60.         // This value will, and must, be set in the dedicated method and nowhere else.
    61.         private float c_InitialDistanceCamToLookTarget;
    62.         private Transform m_CurrentDetachedLookTarget;
    63.         private float c_MediumZoom;
    64.         public const string c_ItemsBaseTag = "Empty";
    65.         public float c_ClampTopCameraAngle = 50.0f;
    66.         #endregion Constants & readonly
    67.         #endregion Members
    68.  
    69.         #region Unity Methods
    70.         private void Start() {
    71.             #region Initialization of gameobjects references
    72.  
    73.             //m_GroundForDetachedMode.SetActive(false);
    74.  
    75.             /*
    76.             if (m_ForceGameobjectsSearch
    77.                 || m_DetachedControlsPanel == null) {
    78.                 m_DetachedControlsPanel = GameObject.Find("Canvas/ControlsPanel");
    79.             }
    80.             if (m_ForceGameobjectsSearch
    81.                 || m_ZoomSlider == null) {
    82.                 m_ZoomSlider = m_DetachedControlsPanel.transform.FindChild("ZoomSlider").GetComponent<Slider>();
    83.             }
    84.  
    85.             if (m_ForceGameobjectsSearch
    86.                 || m_GroundPlacementTarget == null) {
    87.                 m_GroundPlacementTarget = GameObject.Find("Targets/ImageTargets/IT_FloorPlacementTarget");
    88.             }
    89.             */
    90.  
    91.             //Debug.Log("EnteredDetacheMode Start");
    92.  
    93.             EnteredDetachedMode();
    94.  
    95.             //m_BathroomSizePanel.SetActive(false);
    96.             //m_DetachedControlsPanel.SetActive(false);
    97.             #endregion
    98.  
    99.             c_MediumZoom = (m_ZoomSlider.maxValue + m_ZoomSlider.minValue) * 0.5f;
    100.  
    101.             m_BathroomSizeUIScript = m_BathroomSizePanel.GetComponent<BathroomSizeUI>();
    102.         }
    103.         #endregion Unity Methods
    104.  
    105.         #region Detached controls
    106.         public void SetDistancesAndVectors() {
    107.             c_InitialDistanceCamToLookTarget = Vector3.Distance(
    108.                 DetachmentManager.GetInstance.m_DetachedCamera.transform.position,
    109.                 m_CurrentDetachedLookTarget.position);
    110.         }
    111.  
    112.         public void ZoomDetachedCamera() {
    113.             /*if (DetachmentManager.GetInstance.GetCurrentState() != e_EasyBuildStates.DETACHED) {
    114.                 return;
    115.             }*/
    116.  
    117.             Vector3 directionTargetToCam = DetachmentManager.GetInstance.m_DetachedCamera.transform.position
    118.                 - m_CurrentDetachedLookTarget.position;
    119.  
    120.             // We reverse the user input, so we zoom in by filling the slider.
    121.             if (m_ZoomSlider.value == c_MediumZoom) {
    122.                 DetachmentManager.GetInstance.m_DetachedCamera.transform.position = m_CurrentDetachedLookTarget.position
    123.                     + directionTargetToCam.normalized * c_InitialDistanceCamToLookTarget * m_ZoomSlider.value;
    124.             }
    125.             else if (m_ZoomSlider.value < c_MediumZoom) {
    126.                 DetachmentManager.GetInstance.m_DetachedCamera.transform.position = m_CurrentDetachedLookTarget.position
    127.                     + directionTargetToCam.normalized * c_InitialDistanceCamToLookTarget
    128.                     * (c_MediumZoom + c_MediumZoom - m_ZoomSlider.value);
    129.             }
    130.             else {
    131.                 DetachmentManager.GetInstance.m_DetachedCamera.transform.position = m_CurrentDetachedLookTarget.position
    132.                     + directionTargetToCam.normalized * c_InitialDistanceCamToLookTarget
    133.                     * (m_ZoomSlider.minValue + m_ZoomSlider.maxValue - m_ZoomSlider.value);
    134.             }
    135.         }
    136.  
    137.         public void RotateAroundTarget(float horizontalDelta, float verticalDelta) {
    138.             #region Horizontal rotation
    139.             Debug.Log ("float.Epsilon " + float.Epsilon);
    140.             Debug.Log ("horizontalDelta " + horizontalDelta);
    141.             Debug.Log ("verticalDelta " + horizontalDelta);
    142.  
    143.             if (Mathf.Abs(horizontalDelta) > float.Epsilon) {
    144.                 //Debug.Log ("Beacon");
    145.                 Vector3 newPosition =
    146.                     Utils.RotatePointAroundPivot(
    147.                         DetachmentManager.GetInstance.m_DetachedCamera.transform.position,
    148.                         /*m_CurrentDetachedLookTarget.position*/Vector3.zero,
    149.                         -DetachmentManager.GetInstance.m_DetachedCamera.transform.up
    150.                             * c_HorizontalRotationSpeed * horizontalDelta);
    151.  
    152.                 Debug.Log ("Beacon");
    153.  
    154.                 DetachmentManager.GetInstance.m_DetachedCamera.transform.position = newPosition;
    155.             }
    156.             #endregion Horizontal rotation
    157.  
    158.             #region Vertical rotation
    159.             if (Mathf.Abs(verticalDelta) > float.Epsilon) {
    160.                
    161.                 Vector3 newPosition =
    162.                     Utils.RotatePointAroundPivot(
    163.                         DetachmentManager.GetInstance.m_DetachedCamera.transform.position,
    164.                         m_CurrentDetachedLookTarget.position,
    165.                         DetachmentManager.GetInstance.m_DetachedCamera.transform.right
    166.                             * c_VerticalRotationSpeed * verticalDelta);
    167.  
    168.                 // We assert that we don't go bellow the ground.
    169.                 Vector3 newProjectedPosTranslation = Vector3.ProjectOnPlane(newPosition,
    170.                     planeNormal: m_GroundForDetachedMode.transform.up);
    171.                 Vector3 newProjectedPos = m_GroundForDetachedMode.transform.position + newProjectedPosTranslation;
    172.                 float scalarProduct = Vector3.Dot(m_GroundForDetachedMode.transform.up,
    173.                     (newPosition - newProjectedPos).normalized);
    174.  
    175.                 // The scalar product is positive only if the new position is above the ground.
    176.                 if (scalarProduct > 0.0f) {                  
    177.                     Vector3 oldProjectedPosTranslation = Vector3.ProjectOnPlane(
    178.                         DetachmentManager.GetInstance.m_DetachedCamera.transform.position,
    179.                         planeNormal: m_GroundForDetachedMode.transform.up);
    180.                     Vector3 oldProjectedPos = m_GroundForDetachedMode.transform.position + oldProjectedPosTranslation;
    181.                     float oldAngleBetweenCamAndGround = Mathf.Abs(Vector3.Angle(
    182.                         oldProjectedPos - m_GroundForDetachedMode.transform.position,
    183.                         DetachmentManager.GetInstance.m_DetachedCamera.transform.position
    184.                             - m_GroundForDetachedMode.transform.position));
    185.  
    186.                     float newAngleBetweenCamAndGround = Mathf.Abs(Vector3.Angle(
    187.                         newProjectedPos - m_GroundForDetachedMode.transform.position,
    188.                         newPosition - m_GroundForDetachedMode.transform.position));
    189.  
    190.                     // We always allow to go down since we already know we are above the ground.
    191.                     if (oldAngleBetweenCamAndGround - float.Epsilon > newAngleBetweenCamAndGround
    192.                         || newAngleBetweenCamAndGround < c_ClampTopCameraAngle) {
    193.                         DetachmentManager.GetInstance.m_DetachedCamera.transform.position = newPosition;
    194.                     }
    195.                 }
    196.             }
    197.             #endregion Vertical rotation
    198.  
    199.             DetachmentManager.GetInstance.m_DetachedCamera.transform.LookAt(m_CurrentDetachedLookTarget.position,
    200.                 m_GroundForDetachedMode.transform.up);
    201.         }
    202.         #endregion Detached controls
    203.  
    204.         #region Pseudo Events
    205.         public void EnteredDetachedMode() {
    206.             m_BathroomSizePanel.SetActive(false);
    207.             m_DetachedControlsPanel.SetActive(true);
    208.             m_GroundForDetachedMode.SetActive(true);
    209.  
    210.             m_EstimateButton.SetActive(true);
    211.             m_EstimateButton.GetComponentInChildren<Text>().text = "Devis";
    212.  
    213.  
    214.             //this.InitializeDetachedGround();
    215.  
    216.  
    217.  
    218.             m_CurrentDetachedLookTarget = m_GroundForDetachedMode.transform;
    219.             DetachmentManager.GetInstance.m_DetachedCamera.transform
    220.                 .LookAt(m_CurrentDetachedLookTarget.transform.position);
    221.  
    222.             this.SetDistancesAndVectors();
    223.  
    224.             // We reset the slider value
    225.             m_ZoomSlider.value = 1.0f;
    226.  
    227.             //Debug.Log("RotationCoroutine Start");
    228.  
    229.             this.StartCoroutine("RotationCoroutine");
    230.         }
    231.  
    232.         public void ExitDetachedMode() {
    233.             m_DetachedControlsPanel.SetActive(false);
    234.             m_GroundForDetachedMode.SetActive(false);
    235.             m_EstimateButton.SetActive(false);      
    236.  
    237.        
    238.  
    239.             GetFocus.HideAllUI();
    240.  
    241.             //OD.GetComponent<ObjectDetector>().SelectedItems.Clear ();
    242.  
    243.             //Debug.Log("RotationCoroutine Stop");
    244.  
    245.             this.StopCoroutine("RotationCoroutine");
    246.         }
    247.  
    248.         public void DetachedButtonPressed() {
    249.             m_BathroomSizePanel.SetActive(true);
    250.         }
    251.         #endregion Pseudo Events
    252.  
    253.         /*private void InitializeDetachedGround() {
    254.             // Value is stored in real dimension so we need to apply the model's scale.
    255.             Vector2 convertedBathroomSize = new Vector2(
    256.                 Utils.ConvertMetersToPixels(m_BathroomSizeUIScript.m_BathroomSize.x * Utils.c_ModelScale),
    257.                 Utils.ConvertMetersToPixels(m_BathroomSizeUIScript.m_BathroomSize.y * Utils.c_ModelScale));
    258.             // Planes have a scale 10 times superior to Unity's value.
    259.             Vector3 newScale = new Vector3(convertedBathroomSize.x * 0.1f, 1.0f, convertedBathroomSize.y * 0.1f);
    260.  
    261.             // The easiest way to initialize the groud correctly is to put the ground as child of the target
    262.             // and then set relative position and rotation.
    263.             m_GroundForDetachedMode.transform.parent = m_GroundPlacementTarget.transform;
    264.  
    265.             Vector2 ratioScale = new Vector2(
    266.                 newScale.x * 10.0f / m_GroundPlacementTarget.transform.localScale.x,
    267.                 newScale.z * 10.0f / m_GroundPlacementTarget.transform.localScale.z);
    268.             // We clamp to the corner of the image target.
    269.             m_GroundForDetachedMode.transform.localPosition = new Vector3(
    270.                 0.5f - (0.5f * ratioScale.x),
    271.                 c_GroundTargetOffsetY,
    272.                 0.5f - (0.5f * ratioScale.y));
    273.  
    274.  
    275.             m_GroundForDetachedMode.transform.localRotation = Quaternion.identity;
    276.  
    277.             m_GroundForDetachedMode.transform.parent = this.transform;
    278.             // We set the scale according to user's input.
    279.             m_GroundForDetachedMode.transform.localScale = newScale;
    280.  
    281.         }
    282.         */
    283.  
    284.         #region Methods to "clamp" furnitures
    285.  
    286.  
    287.  
    288.  
    289.  
    290.         #endregion Methods to "clamp" furnitures
    291.  
    292.         private IEnumerator RotationCoroutine() {
    293.  
    294.             //Debug.Log("RotationCoroutine ON");
    295.  
    296.             // We wait one frame for the FSM to be updated.
    297.             yield return null;
    298.  
    299.             //Debug.Log (DetachmentManager.GetInstance.GetCurrentState ());
    300.  
    301.             while (DetachmentManager.GetInstance.GetCurrentState() == e_EasyBuildStates.TRACKING) {
    302.                 float horizontalDelta = CrossPlatformInputManager.GetAxisRaw("Horizontal");
    303.                 float verticalDelta = CrossPlatformInputManager.GetAxisRaw("Vertical");
    304.                 this.RotateAroundTarget(horizontalDelta, verticalDelta);
    305.                 //Debug.Log (horizontalDelta);
    306.  
    307.                 m_RotationStick.WallPop();
    308.  
    309.  
    310.  
    311.                 yield return null;
    312.             }
    313.         }
    314.  
    315.         public void SetLookingTarget(Transform newTarget = null) {
    316.             if (newTarget == null) {
    317.                 newTarget = m_GroundForDetachedMode.transform;
    318.             }
    319.  
    320.             m_CurrentDetachedLookTarget = newTarget;
    321.  
    322.             DetachmentManager.GetInstance.m_DetachedCamera.transform.LookAt(m_CurrentDetachedLookTarget.position,
    323.                 m_GroundForDetachedMode.transform.up);
    324.  
    325.             this.ZoomDetachedCamera();
    326.         }
    327.  
    328.     }
    329. }
    330.  
    Inputs:
    Inputs.PNG

    I'm on this issue for a few days now and I can't manage to find and answer. If you have and idea, I would be happy to hear it.

    Thanks!
    [/url][/IMG]
     
  2. Myrmidou

    Myrmidou

    Joined:
    Jun 7, 2014
    Posts:
    30
    Any help? I would really appreciate it!
     
  3. Myrmidou

    Myrmidou

    Joined:
    Jun 7, 2014
    Posts:
    30
    Still the same issue, someone?