Question by
robertbalassan · Sep 26, 2018 at 02:42 PM ·
mobileresolutionjoystickscaledown
Virtual joystick and resolutionScalingFixedDPIFactor problem
Hey, I am trying to use a virtual joystick and also have implemented some downscaling factor for resolution in my android game.
The problem is that when i use any factor lower then 1f my joystick is not working anymore. It just pops and sticks to the left edge right when i touch the screen.
QualitySettings.resolutionScalingFixedDPIFactor = 0.9f;
The virtual joystick scrips are the following:
using UnityEngine;
using UnityEngine.EventSystems;
public class Joystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
[Header("Options")]
[Range(0f, 2f)] public float handleLimit = 1f;
public JoystickMode joystickMode = JoystickMode.AllAxis;
public Transform camera;
protected Vector2 inputVector = Vector2.zero;
[Header("Components")]
public RectTransform background;
public RectTransform handle;
public float Horizontal { get { return inputVector.x; } }
public float Vertical { get { return inputVector.y; } }
public Vector2 Direction { get { return new Vector2(Horizontal, Vertical); } }
public virtual void OnDrag(PointerEventData eventData) { }
public virtual void OnPointerDown(PointerEventData eventData) { }
public virtual void OnPointerUp(PointerEventData eventData) { }
protected void ClampJoystick()
{
if (joystickMode == JoystickMode.Horizontal)
inputVector = new Vector2(inputVector.x, 0f);
if (joystickMode == JoystickMode.Vertical)
inputVector = new Vector2(0f, inputVector.y);
}
}
public enum JoystickMode { AllAxis, Horizontal, Vertical}
and:
using UnityEngine;
using UnityEngine.EventSystems;
public class FixedJoystick : Joystick
{
Vector2 joystickPosition = Vector2.zero;
public static bool isPressed = false;
void Start()
{
joystickPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
public override void OnDrag(PointerEventData eventData)
{
Vector2 direction = eventData.position - joystickPosition ;
inputVector = (direction.magnitude > background.sizeDelta.x /2f ) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
ClampJoystick();
handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
isPressed = true;
}
public override void OnPointerDown(PointerEventData eventData)
{
OnDrag(eventData);
}
public override void OnPointerUp(PointerEventData eventData)
{
inputVector = Vector2.zero;
isPressed = false;
handle.anchoredPosition = Vector2.zero;
}
}
I would like this explained, as I am not looking for someone to just edit the posted code. I appreciate the help!
Comment