I am creating joystick on TouchPhase.Began, and then I am moving the handle on TouchPhase.Moved.
Can’t access direction, horizontal nor vertical from the joystick script since they are read only
(Joystick Pack | Input Management | Unity Asset Store)
So I am trying to do it manually:
else if (t.phase == TouchPhase.Moved)
{
touchLocation thisTouch = touches.Find(touchLocation => touchLocation.touchId == t.fingerId);
thisTouch.handle.position = getTouchPosition(t.position);
Vector2 direction = ((thisTouch.handle.localPosition - thisTouch.handle.localPosition * 0f).normalized);
}
Semi works, problem is that handle can go out of the circle limiter and even slight movement will move the player, is this even right way to do it?
I tried doing something like this:
thisTouch.handle.localPosition = (getTouchPosition(t.position) - (Vector2)thisTouch.handle.position).normalized;
Can I somehow convert touch into actual click and move instead of doing workaround like this?
I would like to use dead zone and handle range variables, otherwise I have to do everything in script, for example this is my idea of dead zone:
float horizontal, vertical;
if (direction.x >= 0.2f || direction.x <= -0.2f)
{
horizontal = direction.x;
}
else horizontal = 0f;
if (direction.y >= 0.2f || direction.y <= -0.2f)
{
vertical = direction.y;
}
else vertical = 0f;
core1.direction = new Vector2(horizontal, vertical);