- Home /
How to stop second finger from manipulating position of gui joystick
I have been working on a 3D mobile app in Unity3D, but have recently come across an issue. I have a joystick that makes my character walk. The joystick is a gui texture that. I have boundaries set so you can't move the joystick all around the screen. It is set up so that when your finger moves outside the boundary, the joystick goes as far as it can go, and when you move your finger back, it continues moving along with your finger. This is good and all but I am going to set it up so that moving another finger along the right side of the screen rotates the camera. The issue happens when one finger is on the joystick and you put another finger on the right side of the screen. The joystick is manipulated by the second finger and even though it isn't touching the joystick it manipulates the position of it. This only happens if a finger is already touching the joystick and you place another finger somewhere else on the screen. I can not figure out how to stop it! Any help will be GREATLY appreciated :) here is my code
#pragma strict
var gui: GUITexture;
var pixelInsetPosResSet: boolean = true;
var playerCharacter: GameObject;
function Start ()
{
guiPixelInsetResSet();
gui.color.a=.1;
gui.transform.position.z=1;
}
function JoystickGUIMove ()
{
// Detecting Touch
if(Input.touchCount > 0 ){
for(var i : int = 0; i < Input.touchCount; i++){
var touch : Touch = Input.GetTouch(i);
if( touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
{
pixelInsetPosResSet = false;
}
if(pixelInsetPosResSet == false)
{
// Moving GUI and Character
gui.pixelInset.x = touch.position.x+Screen.width/(-1.78);
gui.pixelInset.y = touch.position.y+Screen.height/(-1.63);
gui.color.a=.5;
if(gui.pixelInset.y > Screen.height/(-3.5))
{
playerCharacter.transform.position.z = playerCharacter.transform.position.z+(8*Time.deltaTime);
}
else if(gui.pixelInset.y > Screen.height/(-2.6))
{
playerCharacter.transform.position.z = playerCharacter.transform.position.z+(2*Time.deltaTime);
}
else if(gui.pixelInset.y < Screen.height*(-.475))
{
playerCharacter.transform.position.z = playerCharacter.transform.position.z-(2*Time.deltaTime);
}
if(gui.pixelInset.x > Screen.width/(-2.4))
{
playerCharacter.transform.position.x = playerCharacter.transform.position.x+(2*Time.deltaTime);
}
if(gui.pixelInset.x > Screen.width/(-2.4) && gui.pixelInset.y > Screen.height/(-3.5))
{
playerCharacter.transform.position.x = playerCharacter.transform.position.x+(5*Time.deltaTime);
}
if(gui.pixelInset.x < Screen.width/(-2.1))
{
playerCharacter.transform.position.x = playerCharacter.transform.position.x-(2*Time.deltaTime);
}
if(gui.pixelInset.x < Screen.width/(-2.1) && gui.pixelInset.y > Screen.height/(-3.5))
{
playerCharacter.transform.position.x = playerCharacter.transform.position.x-(5*Time.deltaTime);
}
//Setting boundaries
if(pixelInsetPosResSet == false)
{
if((touch.position.y+Screen.height/(-1.63)) > Screen.height/(-4.5))
{
gui.pixelInset.y = Screen.height/(-4.5);
}
if((touch.position.y+Screen.height/(-1.63)) < Screen.height*(-.5))
{
gui.pixelInset.y = Screen.height*(-.5);
}
if((touch.position.x+Screen.width/(-1.78)) > Screen.width/(-2.6))
{
gui.pixelInset.x = Screen.width/(-2.6);
}
if((touch.position.x+Screen.width/(-1.78)) < Screen.width*(-.5))
{
gui.pixelInset.x = Screen.width*(-.5);
}
}
else
{
pixelInsetPosResSet = true;
pixelInsetPositionReset();
}
}
pixelInsetPositionReset();
}
}
}
function pixelInsetPositionReset ()
{
// Reseting Joystick position
for(var i : int = 0; i < Input.touchCount; i++){
var touch : Touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Ended)
{
pixelInsetPosResSet = true;
guiPixelInsetResSet();
}
}
}
function guiPixelInsetResSet ()
{
// Set width and height automatically
if (pixelInsetPosResSet == true)
{
gui.pixelInset.width=Screen.width/8.5;
gui.pixelInset.height=gui.pixelInset.width;
// Set position automatically
gui.pixelInset.x=Screen.width/(-2.25);
gui.pixelInset.y=Screen.height/(-2.35);
}
}
function Update ()
{
pixelInsetPositionReset();
gui.color.a=.1;
JoystickGUIMove ();
}
I'm glad you posted this because I think I have the same issue. It seems that guiTexture.HitTest does not work like it should. For example, if I press on my button, the touch phase is TouchPhase.Began as expected. If I press outside the button nothing registers, implying HitTest is working. However, if I press inside the button and then drag outside, HitTest keeps returning true, no matter where my finger is on the screen.
Your answer
Follow this Question
Related Questions
Multi touches problem 0 Answers
Android 2D multitouch joystick + buttons 0 Answers
Problem with multi touch[solved] 0 Answers
How does multi-touch work 1 Answer
Touch Crosshair problem. 1 Answer