Hello Guest

Author Topic: Virtual Joystick and D Pad for 2dtk 2.2  (Read 21864 times)

hermesdavidms

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 7
    • View Profile
Virtual Joystick and D Pad for 2dtk 2.2
« on: October 11, 2013, 07:54:41 am »
hello

i made a simple virtual joystick that also acts as a directional pad for mobile games, it works with multi touch and is compatible with 2d toolkit 2.2

to use it, just add a sprite, then a ui item and a collider and after that add this script, it accepts a transform if you want a handle to be shown while using the joystick(the circle that follows your finger)  but its optional, make it a child for best results

it doesnt send events or messages, so you will have to poll the info that a couple of functions give you which can be isTouching() or GetPosition() for a normalised(-1.0 to 1.0) joystick input or GetRight() GetUp() and so on to get DPad like input which is just true or false, you can change the deadzone for different results

here is the code

Code: [Select]

using UnityEngine;
using System.Collections;

public class VPad : tk2dUIBaseItemControl
{
public Transform handle;
public bool invertX = false;
public bool invertY = false;


private Vector2 position = Vector2.zero;
private float deadZone = 0.25f;
private bool isTouching = false;
private bool isRight = false;
private bool isLeft = false;
private bool isDown = false;
private bool isUp = false;


void OnEnable()
    {
        if (uiItem)
        {
            uiItem.OnDown += ButtonDown;
        uiItem.OnRelease += ButtonRelease;
        }
    }

void OnDisable()
    {
        if (uiItem)
        {
            uiItem.OnDown -= ButtonDown;
            uiItem.OnRelease -= ButtonRelease;
        }
    }

public void ButtonDown()
    {
isTouching = true;
    }

    public void ButtonRelease()
    {
Reset();
    }

public Vector2 GetPosition()
{
return position;
}

public bool GetRight()
{
return isRight;
}

public bool GetLeft()
{
return isLeft;
}

public bool GetDown()
{
return isDown;
}

public bool GetUp()
{
return isUp;
}

public bool IsTouching()
{
return isTouching;
}

private void Reset()
{
isTouching = false;
if(handle)
{
handle.localPosition = transform.InverseTransformPoint(renderer.bounds.center) + Vector3.back;
}
position = Vector2.zero;
isUp = false;
isDown = false;
isRight = false;
isLeft = false;
}

public void Update()
{
if(isTouching)
{

if (tk2dUIManager.Instance != null)
            {
Camera viewingCamera = tk2dUIManager.Instance.GetUICameraForControl( gameObject );
Vector3 localPos = transform.InverseTransformPoint(viewingCamera.ScreenToWorldPoint((Vector3)uiItem.Touch.position));
localPos.z = -1;

Vector3 padBoundsMin =  transform.InverseTransformPoint(renderer.bounds.min);
Vector3 padBoundsMax =  transform.InverseTransformPoint(renderer.bounds.max);

localPos.x = Mathf.Clamp(localPos.x,padBoundsMin.x,padBoundsMax.x);
localPos.y = Mathf.Clamp(localPos.y,padBoundsMin.y,padBoundsMax.y);

position.x = localPos.x / (renderer.bounds.size.x / 2);
position.y = localPos.y / (renderer.bounds.size.y / 2);
position.x = Mathf.Clamp(position.x , -1.0f,1.0f);
position.y = Mathf.Clamp(position.y , -1.0f,1.0f);

if(handle)
{
handle.localPosition = localPos;
}

if(invertX)
{
position.x = -position.x;
}

if(invertY)
{
position.y = -position.y;
}

if(position.x > deadZone)
{
isRight = true;
}
else
{
isRight = false;
}

if(position.x < -deadZone)
{
isLeft = true;
}
else
{
isLeft = false;
}

if(position.y > deadZone)
{
isUp = true;
}
else
{
isUp = false;
}

if(position.y < -deadZone)
{
isDown = true;
}
else
{
isDown = false;
}

            }
else
{
Reset();
}
}
else
{
Reset();
}
}
}


Cheers
Cheers!
Hermes David Montes de Oca Segovia

scott

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Virtual Joystick and D Pad for 2dtk 2.2
« Reply #1 on: December 13, 2014, 04:12:24 am »
Just wanted to say this worked well for me.  Thanks for posting!