1
\$\begingroup\$

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.

enter image description here

public class QFPSController : MonoBehaviour
{       
    public float mouseSensitivityX = 1.0f;
    public float mouseSensitivityY = 1.0f;
    public float moveScaleX = 1.0f;
    public float moveScaleY = 1.0f;
    public float moveMaxSpeed = 1.0f;
    public float moveLerp = 0.9f;
    public float jumpSpeed = 100f;

    public bool hasPowerUp = false;

    private Rigidbody rigidBody;
    private AudioSource audio;
    private Quaternion rotationTargetHorizontal;
    private Quaternion rotationTargetVertical;
    private Transform cameraTransform;

    void Awake () 
    {
        rotationTargetHorizontal = transform.rotation;
        rigidBody = gameObject.GetComponent<Rigidbody>();
        cameraTransform = transform.GetChild(0);
        rotationTargetVertical = cameraTransform.rotation;
        audio = gameObject.GetComponent<AudioSource>();
    }

    bool jumping=false;

    void Update () 
    {   
        #if UNITY_EDITOR
            Vector3 velocity = (transform.right * Input.GetAxis ("Horizontal") * moveScaleX);
            velocity += (transform.forward * Input.GetAxis ("Vertical") * moveScaleY);
        #else
            Vector3 velocity = (transform.right * CrossPlatformInputManager.GetAxis ("Horizontal") * moveScaleX);
            velocity += (transform.forward * CrossPlatformInputManager.GetAxis ("Vertical") * moveScaleY);
        #endif

        velocity = Vector3.ClampMagnitude (velocity, moveMaxSpeed);
        velocity *= moveLerp;
        velocity.y = rigidBody.velocity.y;
        if (IsGrounded() && !jumping) { //on floor
            rigidBody.velocity = velocity;
        }

        rotationTargetHorizontal *= Quaternion.Euler (0, CrossPlatformInputManager.GetAxis("HorizontalLook") * mouseSensitivityX, 0f);
        transform.rotation = rotationTargetHorizontal;  

        Quaternion nextRotationTargetVertical = rotationTargetVertical * Quaternion.Euler(CrossPlatformInputManager.GetAxis("VerticalLook") *
                                                                                          mouseSensitivityY, 0, 0);
        if (nextRotationTargetVertical.eulerAngles.x < 90 || nextRotationTargetVertical.eulerAngles.x > 270)
        {
            rotationTargetVertical = nextRotationTargetVertical;
            cameraTransform.localRotation = rotationTargetVertical;
        }
        #if UNITY_EDITOR
            if ( Input.GetButtonDown ("Jump") && IsGrounded()) {
        #else
            if ( CrossPlatformInputManager.GetButtonDown ("Jump") && IsGrounded()) {
        #endif
            Debug.Log("Jump!!");
            jumping=true;
            Reset();
            rigidBody.AddForce (0, jumpSpeed,0, ForceMode.Impulse );
        }

        if ( velocity.magnitude > 1f && !audio.isPlaying && IsGrounded()) {
            audio.pitch = Random.Range(0.8f,1.1f);
            audio.volume = Random.Range(0.7f,1f);
            audio.Play();
        }

    }

    void OnCollisionEnter() {
            Debug.Log ("player collision");
            jumping = false;
    }

    public void Reset() {
        if (rigidBody) {
            rigidBody.velocity = Vector3.zero;
            rigidBody.angularVelocity = Vector3.zero;
        }
    }

    public void setRotation(Quaternion rotation)
    {
        this.rotationTargetHorizontal = rotation;
        transform.rotation = rotation;

        rotationTargetVertical = Quaternion.identity;
        cameraTransform.rotation = rotationTargetVertical;
    }

    bool IsGrounded() {
        return Physics.Raycast(transform.position, -Vector3.up, 1.1f);
    }

}

Unity Joystick

using System;
using UnityEngine;
using UnityEngine.EventSystems;

namespace UnityStandardAssets.CrossPlatformInput
{
    public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    {
        public enum AxisOption
        {
            // Options for which axes to use
            Both, // Use both
            OnlyHorizontal, // Only horizontal
            OnlyVertical // Only vertical
        }

        public int MovementRange = 100;
        public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
        public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
        public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input

        Vector3 m_StartPos;
        bool m_UseX; // Toggle for using the x axis
        bool m_UseY; // Toggle for using the Y axis
        CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
        CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input

        void OnEnable()
        {
        }

        void Start()
        {
            CreateVirtualAxes();

            m_StartPos = transform.position;
        }

        void UpdateVirtualAxes(Vector3 value)
        {
            var delta = m_StartPos - value;
            delta.y = -delta.y;
            delta /= MovementRange;
            if (m_UseX)
            {
                m_HorizontalVirtualAxis.Update(-delta.x);
            }

            if (m_UseY)
            {
                m_VerticalVirtualAxis.Update(delta.y);
            }
        }

        void CreateVirtualAxes()
        {
            // set axes to use
            m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
            m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);

            // create new axes based on axes to use
            if (m_UseX)
            {
                m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
                CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
            }
            if (m_UseY)
            {
                m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
                CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
            }
        }


        public void OnDrag(PointerEventData data)
        {
            Vector3 newPos = Vector3.zero;

            if (m_UseX)
            {
                int delta = (int)(data.position.x - m_StartPos.x);
                delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
                newPos.x = delta;
            }

            if (m_UseY)
            {
                int delta = (int)(data.position.y - m_StartPos.y);
                delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
                newPos.y = delta;
            }
            transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
            UpdateVirtualAxes(transform.position);
        }


        public void OnPointerUp(PointerEventData data)
        {
            transform.position = m_StartPos;
            UpdateVirtualAxes(m_StartPos);
        }


        public void OnPointerDown(PointerEventData data) { }

        void OnDisable()
        {
            // remove the joysticks from the cross platform input
            if (m_UseX)
            {
                m_HorizontalVirtualAxis.Remove();
            }
            if (m_UseY)
            {
                m_VerticalVirtualAxis.Remove();
            }
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.