The joystick prefabs available in the Penelope tutorial and iPhone standard assets have a rectangular boundary. Basically what i want is to create a similar behaviour joystick (GUITextures) but with a circular boundary.
(similar to the controls in Minigore or Dungeon Hunter)
Can anyone give me any pointers on how to implement this? Im fairly new to scripting and could not find any example on my previous searches.
Basically to create a circular boundary you just need to limit the distance from a center point to a certain Radius.
So you need to do something like :
var pivot : Vector3; // set this in the editor to be your center point
var radius : float = 10.0; // the radius of the circle
var point : Vector3;
var pointRelatedToPivot : Vector3; // use this to avoid doing the calculation twice
point = someTransform.position; // the location of the handle
pointRelatedToPivot = pivot-point;
if( pointRelatedToPivot.sqrMagnitude>Mathf.Pow(radius,2)){ // i am using sqrMagnitude instead of magnitude since it’s cheaper
point = pivot + pointRelatedToPivot.normalized*radius;
}
someTransform.position = point;
I was able to utilize the code sample from above to set a circular boundary for the penelope joystick controls instead of the default square boundary; however, when the joystick texture gets to the border/perimeter of the circle, it snaps back to its center. Has anyone else encountered this issue?
I already solved this also… well kinda. there is a small bug in mine but i have not found the time to fix it yet.
the basic of what i did is to calculate the distance between the “zero” location and the touch location. then if the distance is larger than X it puts the visual back at the extents. the bug in my system right now is how it puts it back at the extents. it works well but not perfect so i want to make it better.