- Home /
Question by
khaledr · May 13, 2018 at 07:30 PM ·
screen resolutioncrossplatform
How to adjust virtual joystick itself movement (not size) on different mobile resolutions using c# in unity 2d?
How can we adjust virtual joystick ( MobileSingleStickControl / MobileJoystick ) movement on smart phones depending on screen resolution (as shown below) :
I am using MobileSingleStickControl/MobileJoystick component on unity. and I added below variable to Joystick Script:
public int MovementRange = 120;
which determines the maximum range of joystick movements.
The full joystick script is :
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 = 120;
public float delta_x = 0;
public int delta_y = 0;
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()
{
CreateVirtualAxes();
}
void Start()
{
m_StartPos = transform.position;
}
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);
}
}
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);
}
}
public void OnDrag(PointerEventData data)
{
Vector3 newPos = Vector3.zero;
delta_x = 0;
delta_y = 0;
if (m_UseX)
{
int delta = (int)(data.position.x - m_StartPos.x);
delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
delta_x = delta;
newPos.x = delta;
}
if (m_UseY)
{
int delta = (int)(data.position.y - m_StartPos.y);
delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
delta_y = delta;
newPos.y = delta;
}
transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
var newAxisValue = transform.position;
//cancel if joystick is in the below range
if (delta_x > ((-MovementRange /3)) && delta_x < ((MovementRange / 3))) {
newAxisValue.x = m_StartPos.x;//this means ignoring the movement
//Debug.Log (delta_x);
}
if (delta_y > -MovementRange + 10 && delta_y < MovementRange - 10) {
newAxisValue.y = m_StartPos.y;//this means ignoring the movement
}
UpdateVirtualAxes (newAxisValue);
}
public void OnPointerUp(PointerEventData data)
{
transform.position = m_StartPos;
//transform.position = new Vector3(45, 30, 0);
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();
}
}
}
}
I need to set the MovementRange Value Depending on Screen Resolution.
traa7.png
(8.8 kB)
Comment
Your answer
