Here is my script for mobilejoystick object:
using UnityEngine;
using UnityEngine.EventSystems;
using UnitySampleAssets.CrossPlatformInput;
public class JoystickController : MonoBehaviour , IPointerUpHandler , IPointerDownHandler , IDragHandler {
public int MovementRange = 100;
public enum AxisOption
{
Both, // Use both
OnlyHorizontal, // Only horizontal
OnlyVertical // Only vertical
}
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
private Vector3 startPos;
private bool useX; // Toggle for using the x axis
private bool useY; // Toggle for using the Y axis
private CrossPlatformInputManager.VirtualAxis horizontalVirtualAxis; // Reference to the joystick in the cross platform input
private CrossPlatformInputManager.VirtualAxis verticalVirtualAxis; // Reference to the joystick in the cross platform input
private bool moving;
void Start () {
startPos = transform.position;
CreateVirtualAxes ();
}
private void UpdateVirtualAxes (Vector3 value) {
var delta = startPos - value;
delta.y = -delta.y;
delta /= MovementRange;
if(useX)
horizontalVirtualAxis.Update (-delta.x);
if(useY)
verticalVirtualAxis.Update (delta.y);
}
private void CreateVirtualAxes()
{
// set axes to use
useX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
useY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
// create new axes based on axes to use
if (useX)
horizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
if (useY)
verticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
}
public void OnDrag(PointerEventData data)
{
Vector3 newPos = Vector3.zero;
if (useX) {
int delta = (int) (data.position.x - startPos.x);
newPos.x = delta;
}
if (useY)
{
int delta = (int)(data.position.y - startPos.y);
newPos.y = delta;
}
transform.position = Vector3.ClampMagnitude( new Vector3(newPos.x , newPos.y , newPos.z), MovementRange) + startPos;
UpdateVirtualAxes (transform.position);
}
public void OnPointerUp(PointerEventData data)
{
transform.position = startPos;
UpdateVirtualAxes (startPos);
// moving = false;
}
public void OnPointerDown (PointerEventData data)
{
// Debug.Log ("moveDown");
// moving = true;
}
void OnDisable ()
{
// remove the joysticks from the cross platform input
if (useX)
{
horizontalVirtualAxis.Remove();
}
if (useY)
{
verticalVirtualAxis.Remove();
}
}
}
In this mobilejoystick object has horizontalAxisName = “Horizontal”, and verticalAxisName = “Vertical”,which is for move control of left side joystick.
on other side at right,mobilejoystick1 object has horizontalAxisName = “HorizontalLook”, and verticalAxisName = “VerticalLook”,which is for move control of left side joystick.
otherwise both have same script except this changes of above…
i have two joystick controller for mobile game. In that controller-1 is for movement of character and controller-2 is for rotation of same character. But the problem is that if i touch 1st controller then 2nd will not work at same time.So either first or second work at a time.
will you please help me out???