Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Move script for mobile

Discussion in 'Scripting' started by Zamatrius, Jun 30, 2015.

  1. Zamatrius

    Zamatrius

    Joined:
    Mar 25, 2015
    Posts:
    11
    Hi, im doing a simple 2d game, the player can move to the right or to the left,I'm using OnPointerUp and OnPointerDown to read when the player press the button and when he releases the same, i recently found a topic about raycasts, and im thinking if they can be used to read de player input more efficiently and with a lesser cost.
    Here is the scipt that I'm using:
    Code (CSharp):
    1.  
    2.     public float speed = 10;
    3.     Rigidbody2D myBody;
    4.     float hInput = 0;
    5.     float aux;
    6.    
    7.     void Start ()
    8.     {
    9.       //Gerenciar Naves just a simple class to store information about the player: chosen ship, ship speed, hp, etc...
    10.         myBody = this.gameObject.GetComponent<GerenciarNaves> ().Nave_Escolhida ().GetComponent<Rigidbody2D> ();
    11. //NaveEscolhida() return a GameObject(Ship) from a array of ships
    12.         aux = this.gameObject.GetComponent<GerenciarNaves> ().SpeedFactor;
    13.     }
    14.    
    15.     void FixedUpdate ()
    16.     {
    17.         Move (hInput);
    18.     }
    19.    
    20.     void Move(float PInput)
    21.     {
    22.         Vector2 moveVel = myBody.velocity;
    23.         moveVel.x = PInput * speed;
    24.         myBody.velocity = transform.right * (PInput * speed);
    25.     }
    26.    
    27.     public void Direita()//Right
    28.     {
    29.         hInput = aux;
    30.     }
    31.  
    32.     public void Esquerda()//Left
    33.     {
    34.         hInput = (aux * -1);
    35.     }
    36.  
    37.     public void StopMoving()
    38.     {
    39.         hInput = 0;
    40.     }
     
  2. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    So um... what's the problem/question?
     
  3. Zamatrius

    Zamatrius

    Joined:
    Mar 25, 2015
    Posts:
    11
    Raycast can be more efficient to read user inputs or OnPointerUp and OnPointerDown are better choices...