Search Unity

Help to stump basic touch script

Discussion in 'Scripting' started by Mirdov, May 28, 2017.

  1. Mirdov

    Mirdov

    Joined:
    Apr 19, 2017
    Posts:
    5
    Hello, I'm new here and in programming and I would like to know how I can improve this script !?
    In the test I find it a bit slow! At the time of changing the place image.
    My android is the 7.0 = moto g5
    And are testing on HVGA 480x800.
    thanks for the help!

    Code (CSharp):
    1. public class NewBehaviourScript : MonoBehaviour {
    2.  
    3.     public float playX;
    4.     public bool tLeft;
    5.     public bool tRight;
    6.     float Ctela = 240.0f; //screem center
    7.  
    8.     void Start ()
    9.     {
    10.         tRight = true;
    11.  
    12.     }
    13.     // Update is called once per frame
    14.     void Update ()
    15.     {
    16.         if(Input.touchCount > 0)
    17.         {
    18.             if(Input.GetTouch(0).phase == TouchPhase.Began)
    19.             {
    20.                 //variavel play x recebe o valor referente ao toque do lado direito ou esquerdo.
    21.                 playX = Input.GetTouch (0).position.x;
    22.  
    23.                 /*
    24.                  * o valor da variavel Ctela; = centro da tela; se refere a metade da tela de 480x854px
    25.                  * em outro tamanho de tela a variavel Ctela deve ser mudada pela (largura_da_tela / 2)
    26.                  *
    27.                 */
    28.  
    29.                 if(playX > Ctela && tRight == true)
    30.                 {
    31.                     print ("direita");
    32.                     transform.Translate(new Vector2(3.0f,0));
    33.                     tLeft = true;
    34.                     tRight = false;
    35.                 }
    36.                 else if(playX < Ctela && tLeft == true)
    37.                 {
    38.                     print ("esquerda");
    39.                     transform.Translate(new Vector2(-3.0f,0));
    40.                     tRight = true;
    41.                     tLeft = false;
    42.                 }
    43.             }
    44.         }
    45.     }
    46. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not sure what to say, as that script looks straight forward. Nothing jumps out as problematic.