Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
InControl Tap zone for touch controls
using UnityEngine;
namespace InControl
{
public class TouchTapControl : TouchControl
{
[Header("Dimensions")]
public Rect activeArea = new Rect(0.0f, 0.0f, 0.5f, 1.0f);
[Header("Options")]
[Tooltip("Taps to trigger the action")]
public int tapCount = 2;
[Tooltip("Time between two taps")]
public float tapCooldown = 0.5f;
public ButtonTarget target = ButtonTarget.Action1;
private Rect worldActiveArea;
private int currentTapCount;
private float cooldown;
public override void CreateControl()
{
worldActiveArea = TouchManager.ViewToWorldRect(activeArea);
}
void Update()
{
if (cooldown > 0)
{
cooldown -= Time.deltaTime;
if (cooldown <= 0)
{
currentTapCount = 0;
cooldown = 0;
}
}
}
public override void SubmitControlState(ulong updateTick)
{
bool value = false;
if (currentTapCount >= tapCount)
{
value = true;
currentTapCount = 0;
cooldown = 0f;
}
SubmitButtonState(target, value, updateTick);
}
public override void TouchBegan(Touch touch)
{
}
public override void TouchEnded(Touch touch)
{
var beganPosition = TouchManager.ScreenToWorldPoint(touch.position);
var insideActiveArea = worldActiveArea.Contains(beganPosition);
if (insideActiveArea)
{
currentTapCount++;
cooldown = tapCooldown;
}
}
#region Unused methods
public override void DestroyControl()
{
}
public override void ConfigureControl()
{
}
public override void TouchMoved(Touch touch)
{
}
#endregion
#region Debug
public override void DrawGizmos()
{
Utility.DrawRectGizmo(worldActiveArea, Color.yellow);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment