using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class JoystickControllerSystem : MonoBehaviour , IDragHandler , IPointerUpHandler , IPointerDownHandler
{
private Image BgImg;
private Image JoystickImg;
public Vector3 inputVector;
public Vector3 _moveVector;
void Update()
{
Debug.Log (inputVector);
}
void Start()
{
BgImg = GetComponent ();
JoystickImg = transform.GetChild (0).GetComponent ();
}
public virtual void OnDrag (PointerEventData ped)
{
Vector2 pos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(BgImg.rectTransform ,
ped.position , ped.pressEventCamera , out pos))
{
pos.x = (pos.x / BgImg.rectTransform.sizeDelta.x);
pos.y = (pos.y / BgImg.rectTransform.sizeDelta.y);
inputVector = new Vector3 (pos.x* 2 + 1, 0, pos.y * 2 - 1);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
_moveVector = new Vector3 (pos.x* 2 + 1, 0, pos.y * 2 - 1);
_moveVector = (_moveVector.magnitude > 1.0f) ? _moveVector.normalized : _moveVector;
JoystickImg.rectTransform.anchoredPosition = new Vector3 (inputVector.x * (BgImg.rectTransform.sizeDelta.x / 2),
inputVector.z * (BgImg.rectTransform.sizeDelta.y / 2));
}
}
public virtual void OnPointerUp (PointerEventData ped)
{
inputVector = Vector3.zero;
JoystickImg.rectTransform.anchoredPosition = Vector3.zero;
}
public virtual void OnPointerDown (PointerEventData ped)
{
OnDrag (ped);
}
}
I have The Above as my Virtual Joystick…
but iam still not getting the rotation part wich u said(the angle-per-second?)