Search Unity

Standard Assets Cross platform joystick sensitivity

Discussion in 'Scripting' started by dappledore, Dec 14, 2015.

  1. dappledore

    dappledore

    Joined:
    Feb 16, 2015
    Posts:
    12
    I am using the standard asset joystick that comes with Unity 5, the sensitivity changes depending on the device used, for example on an Samsung S3 it is more sensitive than on a Nexus 7 2013, how can i normalize the sensitivity on different devices for the mouse look movement. Also when moving the joystick down, the joystick image jumps up and there is movement upward then when moving the joystick further down it starts to move down , the same when moving to the left.

    Code (CSharp):
    1. public class QFPSController : MonoBehaviour
    2.     {      
    3.         public float mouseSensitivityX = 1.0f;
    4.         public float mouseSensitivityY = 1.0f;
    5.         public float moveScaleX = 1.0f;
    6.         public float moveScaleY = 1.0f;
    7.         public float moveMaxSpeed = 1.0f;
    8.         public float moveLerp = 0.9f;
    9.         public float jumpSpeed = 100f;
    10.  
    11.         public bool hasPowerUp = false;
    12.  
    13.         private Rigidbody rigidBody;
    14.         private AudioSource audio;
    15.         private Quaternion rotationTargetHorizontal;
    16.         private Quaternion rotationTargetVertical;
    17.         private Transform cameraTransform;
    18.  
    19.         void Awake ()
    20.         {
    21.             rotationTargetHorizontal = transform.rotation;
    22.             rigidBody = gameObject.GetComponent<Rigidbody>();
    23.             cameraTransform = transform.GetChild(0);
    24.             rotationTargetVertical = cameraTransform.rotation;
    25.             audio = gameObject.GetComponent<AudioSource>();
    26.         }
    27.  
    28.         bool jumping=false;
    29.  
    30.         void Update ()
    31.         {  
    32.             #if UNITY_EDITOR
    33.                 Vector3 velocity = (transform.right * Input.GetAxis ("Horizontal") * moveScaleX);
    34.                 velocity += (transform.forward * Input.GetAxis ("Vertical") * moveScaleY);
    35.             #else
    36.                 Vector3 velocity = (transform.right * CrossPlatformInputManager.GetAxis ("Horizontal") * moveScaleX);
    37.                 velocity += (transform.forward * CrossPlatformInputManager.GetAxis ("Vertical") * moveScaleY);
    38.             #endif
    39.  
    40.             velocity = Vector3.ClampMagnitude (velocity, moveMaxSpeed);
    41.             velocity *= moveLerp;
    42.             velocity.y = rigidBody.velocity.y;
    43.             if (IsGrounded() && !jumping) { //on floor
    44.                 rigidBody.velocity = velocity;
    45.             }
    46.  
    47.             rotationTargetHorizontal *= Quaternion.Euler (0, CrossPlatformInputManager.GetAxis("HorizontalLook") * mouseSensitivityX, 0f);
    48.             transform.rotation = rotationTargetHorizontal;  
    49.  
    50.             Quaternion nextRotationTargetVertical = rotationTargetVertical * Quaternion.Euler(CrossPlatformInputManager.GetAxis("VerticalLook") *
    51.                                                                                              mouseSensitivityY, 0, 0);
    52.             if (nextRotationTargetVertical.eulerAngles.x < 90 || nextRotationTargetVertical.eulerAngles.x > 270)
    53.             {
    54.                 rotationTargetVertical = nextRotationTargetVertical;
    55.                 cameraTransform.localRotation = rotationTargetVertical;
    56.             }
    57.             #if UNITY_EDITOR
    58.                 if ( Input.GetButtonDown ("Jump") && IsGrounded()) {
    59.             #else
    60.                 if ( CrossPlatformInputManager.GetButtonDown ("Jump") && IsGrounded()) {
    61.             #endif
    62.                 Debug.Log("Jump!!");
    63.                 jumping=true;
    64.                 Reset();
    65.                 rigidBody.AddForce (0, jumpSpeed,0, ForceMode.Impulse );
    66.             }
    67.  
    68.             if ( velocity.magnitude > 1f && !audio.isPlaying && IsGrounded()) {
    69.                 audio.pitch = Random.Range(0.8f,1.1f);
    70.                 audio.volume = Random.Range(0.7f,1f);
    71.                 audio.Play();
    72.             }
    73.  
    74.         }
    75.  
    76.         void OnCollisionEnter() {
    77.                 Debug.Log ("player collision");
    78.                 jumping = false;
    79.         }
    80.  
    81.         public void Reset() {
    82.             if (rigidBody) {
    83.                 rigidBody.velocity = Vector3.zero;
    84.                 rigidBody.angularVelocity = Vector3.zero;
    85.             }
    86.         }
    87.  
    88.         public void setRotation(Quaternion rotation)
    89.         {
    90.             this.rotationTargetHorizontal = rotation;
    91.             transform.rotation = rotation;
    92.  
    93.             rotationTargetVertical = Quaternion.identity;
    94.             cameraTransform.rotation = rotationTargetVertical;
    95.         }
    96.  
    97.         bool IsGrounded() {
    98.             return Physics.Raycast(transform.position, -Vector3.up, 1.1f);
    99.         }
    100.  
    101.     }
    102.  
    103. Unity Joystick
    104.  
    105.  
    106.     using System;
    107.     using UnityEngine;
    108.     using UnityEngine.EventSystems;
    109.  
    110.     namespace UnityStandardAssets.CrossPlatformInput
    111.     {
    112.         public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    113.         {
    114.             public enum AxisOption
    115.             {
    116.                 // Options for which axes to use
    117.                 Both, // Use both
    118.                 OnlyHorizontal, // Only horizontal
    119.                 OnlyVertical // Only vertical
    120.             }
    121.  
    122.             public int MovementRange = 100;
    123.             public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
    124.             public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
    125.             public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
    126.  
    127.             Vector3 m_StartPos;
    128.             bool m_UseX; // Toggle for using the x axis
    129.             bool m_UseY; // Toggle for using the Y axis
    130.             CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
    131.             CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
    132.  
    133.             void OnEnable()
    134.             {
    135.             }
    136.  
    137.             void Start()
    138.             {
    139.                 CreateVirtualAxes();
    140.  
    141.                 m_StartPos = transform.position;
    142.             }
    143.  
    144.             void UpdateVirtualAxes(Vector3 value)
    145.             {
    146.                 var delta = m_StartPos - value;
    147.                 delta.y = -delta.y;
    148.                 delta /= MovementRange;
    149.                 if (m_UseX)
    150.                 {
    151.                     m_HorizontalVirtualAxis.Update(-delta.x);
    152.                 }
    153.  
    154.                 if (m_UseY)
    155.                 {
    156.                     m_VerticalVirtualAxis.Update(delta.y);
    157.                 }
    158.             }
    159.  
    160.             void CreateVirtualAxes()
    161.             {
    162.                 // set axes to use
    163.                 m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
    164.                 m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
    165.  
    166.                 // create new axes based on axes to use
    167.                 if (m_UseX)
    168.                 {
    169.                     m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
    170.                     CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
    171.                 }
    172.                 if (m_UseY)
    173.                 {
    174.                     m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
    175.                     CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
    176.                 }
    177.             }
    178.  
    179.  
    180.             public void OnDrag(PointerEventData data)
    181.             {
    182.                 Vector3 newPos = Vector3.zero;
    183.  
    184.                 if (m_UseX)
    185.                 {
    186.                     int delta = (int)(data.position.x - m_StartPos.x);
    187.                     delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
    188.                     newPos.x = delta;
    189.                 }
    190.  
    191.                 if (m_UseY)
    192.                 {
    193.                     int delta = (int)(data.position.y - m_StartPos.y);
    194.                     delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
    195.                     newPos.y = delta;
    196.                 }
    197.                 transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
    198.                 UpdateVirtualAxes(transform.position);
    199.             }
    200.  
    201.  
    202.             public void OnPointerUp(PointerEventData data)
    203.             {
    204.                 transform.position = m_StartPos;
    205.                 UpdateVirtualAxes(m_StartPos);
    206.             }
    207.  
    208.  
    209.             public void OnPointerDown(PointerEventData data) { }
    210.  
    211.             void OnDisable()
    212.             {
    213.                 // remove the joysticks from the cross platform input
    214.                 if (m_UseX)
    215.                 {
    216.                     m_HorizontalVirtualAxis.Remove();
    217.                 }
    218.                 if (m_UseY)
    219.                 {
    220.                     m_VerticalVirtualAxis.Remove();
    221.                 }
    222.             }
    223.         }
    224.     }
     
  2. zeromilesvega

    zeromilesvega

    Joined:
    Jan 4, 2016
    Posts:
    6
    I'm not sure about the sensitivity issue, I was actually looking for some answers on how to normalize the position from the anchor, make it a circular movement rather than square...

    But i DO know what the problem is with it jumping to that position you were talking about. It has to do with the RectTransform anchor