简体   繁体   中英

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.

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.

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. Can you please give me an example.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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