Once again I’ve found a fault with my joystick script. If I try and change the size of the joystick (like dividing all the values by 2 (so that it is the right size for pre-iphone4 devices)) it gets really messed up. But I cant figure out why its getting messed up. Is there some problem with my script or is it just the way the script is designed? Any help would be greatly appreciated. Thanks.
#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 if(Input.touchCount > (Oid)){
touchID = Oid;
}
else{
touchID = Input.touchCount - 1;
}
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;
}
hours of frustration and lots and lots of math later I come out victorious. It all works now, actually it works better than before, now all you have to do is modify one number in the editor and it will change the size with out modifying the input. Now to figure out how to get it to automatically calculate the touch offset based on position so that its easy to move around.
#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;
var roundTo : float = 10;
var rNumIntX : float;
var rNumIntY : float;
var radiusX : float;
var radiusY : float;
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 if(Input.touchCount > (Oid)){
touchID = Oid;
}
else{
touchID = Input.touchCount - 1;
}
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);// + addX;
purePos.y = (joystick.pixelInset.y - center.y);// + 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{
radiusX = (10/maxDistance)/10;
position.x = Mathf.Round((purePos.x * radiusX) + 0.75);// * roundTo;
}
//Check if in DeadZoneY
if(inDeadZoneY){
position.y = 0;
}
else{
radiusY = (10/maxDistance)/10;
position.y = Mathf.Round((purePos.y * radiusY) + 0.75);// * 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;
}