Matt Yaple
Matt Yaple

Reputation: 65

How to stop second finger from manipulating position of gui joystick in Unity3D

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. When I move one finger along the right side of the screen the joystick gets reset! This is my code... Please Help!

#pragma strict
var gui: GUITexture;
var pixelInsetPosResSet: boolean = true;
var playerCharacter: GameObject;
public var leftID: int;

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))
    {
        leftID = Input.GetTouch(i).fingerId;
        pixelInsetPosResSet = false;
    }
if(pixelInsetPosResSet == false)
{
if(Input.GetTouch(i).fingerId == leftID)
{
// 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 ();
}   

Upvotes: 2

Views: 1608

Answers (2)

Nick
Nick

Reputation: 1035

The simple solution to this is store your left fingerId of touch to a variable say leftId when it hits GUITexture and do the same for right fingerId say rightId. Now when you check for the touch in left joystick check that Input.touches[i].fingerId != rightId and similar for the right joystick check that Input.touches[i].fingerId != leftId.

I have also faced this problem and solved it using this.

Upvotes: 1

Matt Yaple
Matt Yaple

Reputation: 65

Ok, I fixed it! I had to use touch.fingerId instead of Input.GetTouch(i).fingerId took a while, but now it's up and running! :)

Upvotes: 1

Related Questions