Fidel-C
Fidel-C

Reputation: 11

Unity UI buttons mutlitouch

I'm building a pong game for android devices. So I insert a canvas with three buttons. The first button is to move the player paddle up, the second button is to move the player button down. While the third button is to boost the ball speed.

I'm using event trigger for the three buttons with pointer up and pointer down.

public float playspeed = 0.3f;
public Vector2 player1pos = new Vector2 ( 0 , 0 ) ;

bool paddlemoveup;
bool paddlemovedown;


void Update () 
{

if ( paddlemoveup == true)

{
         float ypos = gameObject.transform.position.y + Vector2.right.y * playspeed;

        player1pos = new Vector2 (-3.6f, Mathf.Clamp(ypos, -1.7f, 1.7f)) ;
        gameObject.transform.position = player1pos ;


}

if ( paddlemovedown == true )

{
         float ypos = gameObject.transform.position.y + Vector2.left.y * playspeed;
        player1pos = new Vector2 (-3.6f, Mathf.Clamp(ypos, -1.7f, 1.7f),) ;
        gameObject.transform.position = player1pos ;


}

}  

public void UPOnPointerDown ()

{

    paddlemoveup = true;

} 

public void UpOnPointerUp ()

{

    paddlemoveup = false;

}

public void DownOnPointerDown ()

{

    paddlemovedown = true;

} 

public void DownOnPointerUp ()

{

    paddlemovedown = false;

}

}

I'm using the previous code for the player paddle to move up and down.

And for the boost button, I'm using the following code >>>

    void OnCollisionEnter2D (Collision2D col)

{

    if (col.gameObject.tag == "player1" && booster == true )
    {

       ballspeed = ballspeed + 0.5f;
       } public void Boostdown ()

{

    booster = true;

}

public void Boostup ()

{

    booster = false; 

}

    }

Now my problems or questions are these:

1- Is the UI button is the best way that I can use for my game, or there is a better way that I can use for this kind of games to use on touch devices (Android and IOS).

2- When I'm testing the game on android devices I tried to touch the boost button and the ( right or left buttons), but the problems I can't touch two buttons at the same time. So how can I fix this problem?

3- Regarding the ball boost script, is this the right way of doing it or is there a better way, and how can I boost the ball speed for let say 2 seconds, and after that return to the previous ball speed.

4-lastly, how I can set a timer for the boost button so that the player can only press the boost button one every 15 seconds.

Sorry for asking so many stupid questions

Thanks :)

Upvotes: 0

Views: 86

Answers (1)

Moh97
Moh97

Reputation: 73

I don't think the UI is the best choice to make a game controller you can use Input.touches to know where is your fingers and how many finger you have in the screen and you can know the fingerId so you can track the same finger https://docs.unity3d.com/ScriptReference/Input-touches.html take a look

Upvotes: 0

Related Questions