debugging - 如何阻止第二根手指在Unity3D中操纵gui操纵杆的位置

标签 debugging mobile touch unity-game-engine multi-touch

我一直在 Unity3D 中开发 3D 移动应用程序,但最近遇到了一个问题。我有一个操纵杆可以让我的角色行走。操纵杆是一个 GUI 纹理。我设置了边界,因此您无法在屏幕上移动操纵杆。它的设置是这样的:当您的手指移出边界时,操纵杆会尽可能地移动,而当您向后移动手指时,操纵杆会继续与您​​的手指一起移动。这很好,但我将对其进行设置,以便沿着屏幕右侧移动另一根手指来旋转相机。当一根手指放在操纵杆上而另一根手指放在屏幕右侧时,就会出现此问题。当我沿着屏幕右侧移动一根手指时,操纵杆就会重置!这是我的代码...请帮忙!

#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 ();
}   

最佳答案

好的,我修好了!我必须使用 touch.fingerId 而不是 Input.GetTouch(i).fingerId 花了一段时间,但现在它已经启动并运行了! :)

关于debugging - 如何阻止第二根手指在Unity3D中操纵gui操纵杆的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19171611/

相关文章:

objective-c - 如何检测 subview 中的点击手势

ios - 只要用户正在交互就显示菜单

java - 如何告诉 Eclipse 使用来自不同库的不同版本的包进行构建?

html - 粘性页脚不适用于移动设备

html - Sencha 触摸 : How the heck does this framework work?

java - 使用编程在android上设置触摸输入

java - 亚马逊 linux - 安装 openjdk-debuginfo?

java - 程序崩溃。破译错误信息

iphone - 取消引用空指针,警告

debugging - 调试时将值存储在 gdb 中