简体   繁体   English

Unity多点触控,如何使用touch.fingerid

[英]Unity multitouch, How to use touch.fingerid

I'm trying to build a simple pong game for touch devices (Android and IOS), and this is my first project, I'm really beginner into this thing and I need help, please. 我正在尝试为触摸设备(Android和IOS)构建一个简单的乒乓球游戏,这是我的第一个项目,我真的是初学者,我需要帮助,请。

there will be three buttons on the screen. 屏幕上会有三个按钮。 The first button is to move the player paddle up, the second button is to move the paddle down and the third button is to boost the ball speed for 2 seconds when the ball touches the player paddle and in the same time the boost button been touched. 第一个按钮是向上移动玩家桨,第二个按钮用于向下移动球拍,第三个按钮用于在球接触球员球拍时提高球速2秒,同时触发增强按钮。

this is my script for the player paddle 这是我的播放器拨片脚本

public class player1 : MonoBehaviour {

    float playspeed = 15f;
    float player1pos;

    Ray2D ray;
    RaycastHit2D hit;

    Rigidbody2D rb;

    Vector2 paddlepos;

    GameObject paddle1;

        void Update () 
        { 

        rb = gameObject.GetComponent <Rigidbody2D> ();

        paddlepos = transform.position;
        paddlepos.y = Mathf.Clamp ( transform.position.y , -9f , 9f );
        transform.position = paddlepos;

        if (Input.touchCount > 0 )
        {
            for ( int i = 0; i < Input.touchCount; i++)
            {  
                if ( Input.GetTouch(i).phase == TouchPhase.Began )
                {

                    Vector2 point = Camera.main.ScreenToWorldPoint (Input.GetTouch(i).position);
                    hit = Physics2D.Raycast (point , Vector2.zero, 10);


                    if ( hit.collider != null )
                    {

                    if (hit.collider.name == "up" )

                    {
                            rb.velocity = new Vector2 ( 0 , playspeed );                                
                    }

                    if (hit.collider.name == "down" )

                    {
                    rb.velocity = new Vector2 ( 0 , -playspeed);     
                    }       
                }       

                if ( Input.GetTouch(i).phase == TouchPhase.Ended )
                {
                    rb.velocity = new Vector2 (0 , 0);  
                }           
    }
   }
  }
 }  
}

And for the ball boost button I'm using the following code 对于球提升按钮,我使用以下代码

public class ball : MonoBehaviour {

    public float ballspeed = 7f;

    Ray2D ray;
    RaycastHit2D hit;

   float previousspeed;

IEnumerator ballbooster()
    {   
        previousspeed = ballspeed;

        ballspeed = ballspeed * 1.8f ;

        yield return new WaitForSeconds (2);

        ballspeed = previousspeed;

        yield break;
    } 

void Update ()

    {
        if (Input.touchCount > 0 )
        {
            for ( int i = 0; i < Input.touchCount; i++)
            {  
                if ( Input.GetTouch(i).phase == TouchPhase.Began )
                {

                    Vector2 point = Camera.main.ScreenToWorldPoint (Input.GetTouch(i).position);
                    hit = Physics2D.Raycast (point , Vector2.zero, 10);

                    if ( hit.collider != null )
                    {
                        if (hit.collider.name == "boost" && distance < 5)
                        {
                            StartCoroutine (ballbooster());
                        }   
                    }

     }
    }
   }
  }
 }
}

My problem is that I can't use multitouch, for example when I touch the up button and then press the boost button the paddle stop moving. 我的问题是我无法使用多点触控,例如当我触摸向上按钮然后按下提升按钮时,拨片停止移动。 Now I'm trying to handle multitouch. 现在我正在尝试处理多点触控。 I know that I have to use touch.fingerid to do what I want, but I don't know how to do that in code. 我知道我必须使用touch.fingerid来做我想做的事,但我不知道如何在代码中做到这一点。 Can you please give me an example. 你能给我举个例子吗?

您是否注意到touchphase.ended位于touchphase.began内?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM