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. Dismiss Notice

Android phone controlls

Discussion in 'Scripting' started by mholmes, Dec 18, 2020.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    I'm not 100% sure how to move my char on the phone. Building a shooter app. I have it working for controls and keyboard and mouse. Now I need to get it for touch screens.

    code:
    Code (CSharp):
    1.  // Update is called once per frame
    2.     void Update()
    3.     {
    4.         //HUD
    5.         _Score.text = Game_Data._Score.ToString();
    6.         _Kill_Count.text = Game_Data._Kill_Count.ToString();
    7.  
    8.         //Ship Movement
    9.         if (Game_Data._Movement_Use_Mouse)
    10.         {
    11.             TargetFollow = Input.mousePosition;
    12.             TargetFollow = Camera.main.ScreenToWorldPoint(TargetFollow);//Convert Mouse Postion To World Postion
    13.  
    14.             TargetFollow.z = transform.position.z;
    15.             transform.position = Vector3.Lerp(transform.position, TargetFollow, speed * Time.deltaTime); // Move object to pointer
    16.         }
    17.         else
    18.         {
    19.             rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * speed, 0));
    20.             rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * speed));
    21.         }
    22.  
    23.         //Fire Weapon
    24.         if (Game_Data._Semi_Auto_Fire)
    25.         {
    26.             //Mouse
    27.             if (Input.GetMouseButton(0) && !_DelayFire) { Fire_Laser(); }
    28.             if (Input.GetMouseButton(1) && !_DelayFire) { Fire_Laser(); }
    29.  
    30.             //Keyoard
    31.             if (Input.GetKey(KeyCode.Space) && !_DelayFire) { Fire_Laser(); }
    32.  
    33.             //Joystick
    34.             if(Input.GetKey(KeyCode.Joystick1Button0) && !_DelayFire) { Fire_Laser(); }
    35.         }
    36.         else
    37.         {
    38.             //Mouse
    39.             if (Input.GetMouseButton(0) && !_DelayFire) { Fire_Laser(); }
    40.  
    41.             //Keyboard
    42.             if (Input.GetKeyDown(KeyCode.Space) && !_DelayFire) { Fire_Laser(); }
    43.  
    44.             //Joystick
    45.             if(Input.GetKeyDown(KeyCode.Joystick1Button0) && !_DelayFire) { Fire_Laser(); }
    46.         }
    47.     }
    48.  
    49.     #region"Phone"
    50.     public void Move_Left()
    51.     {
    52.         rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * speed, 0));
    53.     }
    54.  
    55.     public void Move_Right()
    56.     {
    57.         rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * speed, 0));
    58.     }
    59.  
    60.     public void Move_Up()
    61.     {
    62.         rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * speed));
    63.     }
    64.  
    65.     public void Move_Down()
    66.     {
    67.         rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * speed));
    68.     }
    69.  
    70.     public void Laser()
    71.     {
    72.         if (!_DelayFire) { Fire_Laser(); }
    73.     }
    74.     #endregion
     
  2. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    Tried this but still not working
    Code (CSharp):
    1.  void Update()
    2.     {
    3.         //HUD
    4.         _Score.text = Game_Data._Score.ToString();
    5.         _Kill_Count.text = Game_Data._Kill_Count.ToString();
    6.  
    7.         //Ship Movement
    8.         if (Game_Data._Movement_Use_Mouse)
    9.         {
    10.             TargetFollow = Input.mousePosition;
    11.             TargetFollow = Camera.main.ScreenToWorldPoint(TargetFollow);//Convert Mouse Postion To World Postion
    12.  
    13.             TargetFollow.z = transform.position.z;
    14.             transform.position = Vector3.Lerp(transform.position, TargetFollow, speed * Time.deltaTime); // Move object to pointer
    15.         }
    16.         else if (Input.GetButtonDown("btnArrowRight") || Input.GetButton(KeyCode.D.ToString()))
    17.         {
    18.             transform.position += Vector3.right * speed * Time.deltaTime;
    19.         }
    20.         else if (Input.GetButtonDown("btnArrowLeft") || Input.GetButton(KeyCode.A.ToString()))
    21.         {
    22.             transform.position += Vector3.left * speed * Time.deltaTime;
    23.         }
    24.         else if (Input.GetButtonDown("btnArrowUp") || Input.GetButton(KeyCode.W.ToString()))
    25.         {
    26.             transform.position += Vector3.up * speed * Time.deltaTime;
    27.         }
    28.         else if (Input.GetButtonDown("btnArrowDown") || Input.GetButton(KeyCode.S.ToString()))
    29.         {
    30.             transform.position += Vector3.down * speed * Time.deltaTime;
    31.         }
    32.         else
    33.         {
    34.             rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * speed, 0));
    35.             rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * speed));
    36.         }
    37.  
    38.         //Fire Weapon
    39.         if (Game_Data._Semi_Auto_Fire)
    40.         {
    41.             //Mouse
    42.             if (Input.GetMouseButton(0) && !_DelayFire) { Fire_Laser(); }
    43.             if (Input.GetMouseButton(1) && !_DelayFire) { Fire_Laser(); }
    44.  
    45.             //Keyoard
    46.             if (Input.GetKey(KeyCode.Space) && !_DelayFire) { Fire_Laser(); }
    47.  
    48.             //Joystick
    49.             if(Input.GetKey(KeyCode.Joystick1Button0) && !_DelayFire) { Fire_Laser(); }
    50.         }
    51.         else
    52.         {
    53.             //Mouse
    54.             if (Input.GetMouseButton(0) && !_DelayFire) { Fire_Laser(); }
    55.  
    56.             //Keyboard
    57.             if (Input.GetKeyDown(KeyCode.Space) && !_DelayFire) { Fire_Laser(); }
    58.  
    59.             //Joystick
    60.             if(Input.GetKeyDown(KeyCode.Joystick1Button0) && !_DelayFire) { Fire_Laser(); }
    61.         }
    62.     }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    I have a bunch of full touch controls examples in my proximity_buttons repo.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/

    If you want to debug the above, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  4. Havyx

    Havyx

    Joined:
    Oct 20, 2020
    Posts:
    140
  5. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    do you have code snipets? that would be prefered or review my code and offer direct snippet feedback suggestion
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Code is like 10% of the problem. The rest is setup. The best code in the world won't work if your input is setup wrong.

    Glancing at the above code, all I can say is "yeah, looks fine, probably fine, but what data is it processing?"

    And THAT cannot be determined without the code running in context.

    And guess who here is most-well-positioned to instrument the code in question (perhaps with Debug.Log() statements?) to find where things are failing?
     
  7. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    I came up with this for touch screen so far but I'm going to work toward on screen controller i think

    Code (CSharp):
    1. //Phone & Touch
    2.         if (WBG_Config._Build == "Phone")
    3.         {
    4.             TargetFollow = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    5.             transform.position = Vector3.Lerp(transform.position, TargetFollow, speed * Time.deltaTime);
    6.         }