- Home /
Constrain touch joystick around a point
Right now, when you touch the joystick, that touch is 'latched' to the joystick, and it will follow your finger around until you release the joystick, thereby 'unlatching' the touch and allowing the stick to return to it's 'home' position. The last two images are what I am trying to achieve; I think what I need to so is use the location of the joystick and the orient to calculate the distance between the two and force the distance to max out at a specified length regardless of the position of my touch, but I am not sure how to calculate that. Am I going down the right road here?
var joyOrient : GameObject;
var joyButton : GUITexture;
var joyBorder : GUITexture;
var xValue : float;
var yValue : float;
private var savedPosition : Vector3;
private var orientPosition : Vector3;
private var joyTouchID : int;
private var latched : boolean;
function Awake () {
orientPosition = joyOrient.transform.position; //set orient to location of joyOrient object
} //End Awake
function Update () {
for (var event : Touch in Input.touches) { //check for touches
var JoyHitTest = (joyButton.HitTest(event.position)); //did you touch the joystick?
if (event.phase == TouchPhase.Began || event.phase == TouchPhase.Moved) { //if phase is Began or Moved
if(JoyHitTest){ //and if you touched the joystick thumb
latched = true; //touch is latched to stick
} //End if
} //End if
if (event.phase == TouchPhase.Ended && event.fingerId == joyTouchID){ //if the touch ends, and it is the touch used to latch the joystick
latched = false;
} //End If
if (latched){ //this is the meat, and where I am sure I need to make my edits to constrain the distance the thumb will move
joyTouchID = event.fingerId; //get ID of latched touch
joyButton.transform.position.x = (event.position.x/1024); //set position of thumb to finger location on iPad screen
joyButton.transform.position.y = (event.position.y/768); //must change this so resolution is variable driven
joyButton.transform.position.z = 0; //z axis is always zero since we only care about 2D movement here
savedPosition = joyButton.transform.position; //save current location into a Vec3
xValue = orientPosition.x-savedPosition.x;
yValue = orientPosition.y-savedPosition.y; //calculate X/Y value. Needs work, to include normalization of output
} //End If
else if (!latched){
joyButton.transform.position = orientPosition; //return joystick thumb to the orient location
}//End If
} //End for
} //End Update
Comment