Hi there,
I’m trying to make a joystick script that measures left right up and down. At the moment I am measuring the pointer’s position over a square area where x = 0 to 1 from left to right and y = 0 to 1 from botton to top.
This is working really well for placing graphics at the right spot and most things but I cant find a good way to measure when it is in each of the zones based on that x,y data.
so in the meantime I stuck with a patheticly lame script (please don’t mock me for it ).
function RightButtonPosCheck(){
if (RightButtonPosVector2.y >= .75)RightButtonPosition = 1;//Up
if (RightButtonPosVector2.y <= .25)RightButtonPosition = 3;//Down
if (RightButtonPosVector2.x <= .25)RightButtonPosition = 4;//Left
if (RightButtonPosVector2.x >= .75)RightButtonPosition = 2;//Right
}
Any way it works but it’s pretty crappy, does anyone have any good suggestions on how to detect when the pointer is in those zones. I just want it to feed bak a 1-4 value for me…
measure distance to center → radius. if larger than radius of the inner circle, you know you are in the zone
use the angle of the direction center → touch in relation to “right” to define the sector ( Mathf.Sign(yPos - centerY)*acos( xPos - centerX), which gives you angle from 0 to ± 180 deg)
Acos() only returns an angle in the range [0, pi], as that’s all that can be determined from the cosine alone. The sign of the y coordinate is then used to determine whether the actual angle falls in the range [0, pi] or (-pi, 0).
However, I would just use Atan2() instead. That way you don’t have to worry about determining the sign, and you also don’t have to worry about the input falling outside the range [-1, 1] due to numerical imprecision (which may not be an issue here, but with Atan2() you’ll be safe either way).