Ok a while back I posted about a circular clamped Joystick, and got it working perfectly. Lately I did some durability testing on it, and found a problem that I can’t seem to fix. I have two joysticks on screen, so if I hold down one, its touchID = 0, and if I then hold down the other one then the other ones touchID = 1, that all works right, but then if I take my finger off of the joystick whose touchID is zero, the other joysticks touchID is supposed to go to zero, and the original one with a touchID of zero is supposed to go to -1, but what happens is both go to -1, and I can’t figure out why. Any help would be greatly appreciated.
EDIT: Fixed Code is Posted Below
Nevermind, I got it fixed, it works perfect now. Heres the final revised code.
#pragma strict
var joystick : GUITexture;
var jBG : GUITexture;
var otherJ : JoystickTest5;
var jOffset : Vector2;
var position : Vector2;
var minDeadZone : Vector2 = Vector2(-5, -5);
var maxDeadZone : Vector2 = Vector2(5, 5);
var bgSize : float;
var latched : boolean = false;
private var Oid : int;
private var maxDistance : float;
private var roundTo : float = 10;
private var addX : float;
private var addY : float;
var touchID : int;
private var touch : Touch;
private var tCount : int = 0;
private var inDeadZoneX : boolean = true;
private var inDeadZoneY : boolean = true;
private var guiTouchPos : Vector2;
private var dist : float;
private var dirVector : Vector2;
private var clampedPos : Vector2;
private var center : Vector2;
private var jHit : boolean;
private var purePos : Vector2;
private var activated : boolean = false;
function Start(){
jBG.pixelInset.width = bgSize;
jBG.pixelInset.height = bgSize;
joystick.pixelInset.width = bgSize / 2;
joystick.pixelInset.height = bgSize / 2;
center.x = joystick.pixelInset.x + (joystick.pixelInset.width/2);
center.y = joystick.pixelInset.y + (joystick.pixelInset.height/2);
jBG.pixelInset.x = center.x - (jBG.pixelInset.width/2);
jBG.pixelInset.y = center.y - (jBG.pixelInset.height/2);
maxDistance = (jBG.pixelInset.width/2) / 1.5;
addX = (jBG.pixelInset.width/1000) + 0.25;
addY = (jBG.pixelInset.height/1000) + 0.25;
}
function Update(){
for(var i = 0; i < Input.touchCount; i++){
var realTouch : Touch = Input.touches[i];
guiTouchPos = touch.position - jOffset;
jHit = joystick.HitTest(realTouch.position);
if(realTouch.phase == TouchPhase.Began){
if(jHit){
latched = true;
touchID = realTouch.fingerId;
Oid = touchID;
}
}
if(latched){
if(Input.touchCount <= 1){
if(touchID >= 1){
touchID = 0;
}
}
else{
touchID = Oid;
}
touch = Input.touches[touchID];
if(touch.phase == TouchPhase.Ended){
if(!jHit){
latched = false;
touchID = -1;
}
}
}
}
if(latched){
clampedPos = GetConstrainedPosition(center, guiTouchPos);
if(activated){
joystick.pixelInset.x = clampedPos.x - (joystick.pixelInset.width/2);
joystick.pixelInset.y = clampedPos.y - (joystick.pixelInset.height/2);
}
else{
joystick.pixelInset.x = guiTouchPos.x - (joystick.pixelInset.width/2);
joystick.pixelInset.y = guiTouchPos.y - (joystick.pixelInset.height/2) ;
}
}
else{
joystick.pixelInset.x = center.x - (joystick.pixelInset.width/2);
joystick.pixelInset.y = center.y - (joystick.pixelInset.height/2);
}
purePos.x = ((joystick.pixelInset.x - center.x)/ 100) + addX;
purePos.y = ((joystick.pixelInset.y - center.y)/ 100) + addY;
if(purePos.x > 0){
purePos.x += addX;
}
else{
purePos.x -= addX;
}
if(purePos.y > 0){
purePos.y += addY;
}
else{
purePos.y -= addY;
}
if(purePos.x < maxDeadZone.x){
if(purePos.x > minDeadZone.x){
inDeadZoneX = true;
}
else{
inDeadZoneX = false;
}
}
else{
inDeadZoneX = false;
}
if(purePos.y < maxDeadZone.y){
if(purePos.y > minDeadZone.y){
inDeadZoneY = true;
}
else{
inDeadZoneY = false;
}
}
else{
inDeadZoneY = false;
}
//Check if in DeadZoneX
if(inDeadZoneX){
position.x = 0;
}
else{
position.x = Mathf.Round(purePos.x * roundTo) / roundTo;
}
//Check if in DeadZoneY
if(inDeadZoneY){
position.y = 0;
}
else{
position.y = Mathf.Round(purePos.y * roundTo) / roundTo;
}
}
function GetConstrainedPosition(midPoint : Vector2, endPoint : Vector2)
{
dist = Vector2.Distance(midPoint, endPoint);
if (Vector2.Distance(midPoint, endPoint) > maxDistance)
{
activated = true;
//the touch is outside the radius. Return the clamped position.
dirVector = endPoint - midPoint;
dirVector.Normalize();
return (dirVector * maxDistance) + midPoint;
}
//this returns the touched position if we're inside the radius.
activated = false;
return clampedPos;
}