- Home /
Question by
aleksa_stevic · Nov 03, 2014 at 09:10 PM ·
joysticky-axisended
Joystick wont'go to it's default position when touch end
I created a script that will move the joystick along the touch position. The joystick will move just along Y axis, X axis is always 0.1 . When touch is ended the joystick should return to default position. I works, exept in the cases when a joystick is in limit of the Y axis, and when I move the finger a little bit on the X axis. Please, can someone explain to me why, and help me fix this.. Sorry for my bad english.
#pragma strict
//A class that will hold the boundary
class Limit
{
var min = 0.3;
var max = 0.7;
}
var relativePosition : float;
//Variable that will hold the joystick default position
private var joystickDefaultPos : Vector2;
//Create an instance of the class:
var limit = new Limit();
function Start ()
{
joystickDefaultPos = transform.position;
}
function Update ()
{
//If the user touched the screen
if (Input.touchCount > 0)
{
//How many touches do we have?
for (var i : int; i < Input.touches.Length; i++)
{
//Declare the touch variable tha will hold current touch:
var touch : Touch = Input.touches[i]; //The touch
//If we touch the guiTexture:
if (guiTexture.HitTest(touch.position))
{
//If the user moved the finger
if (touch.phase == TouchPhase.Moved)
{
var positionX = 0.1;
var positionYTranslation = Camera.main.ScreenToViewportPoint(Vector3(0.1, touch.position.y, 0));
var positionY = Mathf.Clamp(positionYTranslation.y, limit.min, limit.max);
transform.position = Vector2(positionX, positionY);
}
if (touch.phase == TouchPhase.Ended)
{
//Wen the user lifts the finger of the screen:
transform.position = joystickDefaultPos;
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
5th axis on joystick does not working with the main camera 0 Answers
Joystick , deleting corner movements ? 1 Answer
My gamepad axes data is somebody elses, or CMB? 2 Answers
how to rotate object with joystick (Mobile) 0 Answers
Phantom Joystick Input 3 Answers