Search Unity

Feedback Touch control script is not working properly

Discussion in '2D' started by Yadriel, Sep 19, 2019.

  1. Yadriel

    Yadriel

    Joined:
    Sep 19, 2019
    Posts:
    1
    Hello everyone.
    First of all - I am new to scripting. Actually, I am working on my first ever project, which is mostly being built with tutorials and premade assets. The thing is, I want to build simple 2D (or maybe Top-Down suits better) RPG game for Android, and I am struggling with mobile controls. Script that's responsible for movement works perfectly on keyboard while testing in Editor, but additional script for touch controls is not working at all. There is no error message, game is starting, but buttons I've implemented to the game are not responding, while arrow keys still allow to move player. Here is code I am using.

    Player Movement script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Controls : MonoBehaviour {
    4.     public Rigidbody2D rb;
    5.     public float speed;
    6.     public bool moveup;
    7.     public bool moveleft;
    8.     public bool moveright;
    9.     public bool movedown;
    10.  
    11.     public Vector2 direction;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody2D>();
    17.     }
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         GetInput();
    22.         Move();  
    23.     }
    24.      
    25.    
    26.     public void Move()
    27.     {
    28.         transform.Translate(direction*speed*Time.deltaTime);
    29.     }
    30.     public void GetInput()
    31.     {
    32.  
    33.         direction = Vector2.zero;
    34.  
    35.         if (Input.GetKey(KeyCode.UpArrow))
    36.         {
    37.         direction += Vector2.up;
    38.         }
    39.  
    40.         if (Input.GetKey(KeyCode.LeftArrow))
    41.         {
    42.         direction += Vector2.left;
    43.         }
    44.  
    45.  
    46.         if (Input.GetKey(KeyCode.RightArrow))
    47.         {
    48.         direction += Vector2.right;
    49.         }
    50.  
    51.  
    52.         if (Input.GetKey(KeyCode.DownArrow))
    53.         {
    54.         direction += Vector2.down;
    55.         }
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62.         if (moveup)
    63.         {
    64.         direction += Vector2.up;
    65.         }
    66.  
    67.         if (moveleft)
    68.         {
    69.         direction += Vector2.left;
    70.         }
    71.  
    72.  
    73.  
    74.         if (moveright)
    75.         {
    76.         direction += Vector2.right;
    77.         }
    78.  
    79.  
    80.         if (movedown)
    81.         {
    82.         direction += Vector2.down;
    83.         }
    84.     }
    85. }


    Second script, which was supposed to allow buttons added to UI to move player:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Touch : MonoBehaviour
    4. {
    5.     private Controls player;
    6.             void Start()
    7.             {
    8.                 player = FindObjectOfType<Controls>();
    9.             }
    10.             public void UpArrow()
    11.             {
    12.                 player.moveright = false;
    13.                 player.moveleft = false;
    14.                 player.moveup = true;
    15.                 player.movedown = false;
    16.             }
    17.          
    18.          
    19.          
    20.          
    21.             public void LeftArrow()
    22.             {
    23.                 player.moveright = false;
    24.                 player.moveleft = true;
    25.                 player.moveup = false;
    26.                 player.movedown = false;
    27.             }
    28.          
    29.          
    30.             public void RightArrow()
    31.             {
    32.                 player.moveright = true;
    33.                 player.moveleft = false;
    34.                 player.moveup = false;
    35.                 player.movedown = false;
    36.             }
    37.          
    38.          
    39.          
    40.             public void DownArrow()
    41.             {
    42.                 player.moveright = false;
    43.                 player.moveleft = false;
    44.                 player.moveup = false;
    45.                 player.movedown = true;
    46.             }
    47.          
    48.          
    49.          
    50.          
    51.             public void ReleaseUpArrow()
    52.             {
    53.              
    54.                 player.moveup = false;
    55.      
    56.             }
    57.          
    58.             public void ReleaseLeftArrow()
    59.             {
    60.                 player.moveleft = false;
    61.      
    62.             }        
    63.          
    64.             public void ReleaseRightArrow()
    65.             {
    66.                 player.moveright = false;
    67.              
    68.             }        
    69.          
    70.             public void ReleaseDownArrow()
    71.             {
    72.                 player.movedown = false;
    73.             }
    74.         }


    I have also added events to each button, regarding to their functionality, which are triggered by MousePointerDown and cancelled by MousePointerUp events.




    Buttons are not visible on scren, since my UI Canvas is scaled to the top right corner of window, but they are shown in Hierarchy to the left.

    I am stuck at very beginning of this project, but I am afraid to continue before solving this, so I want to ask you for help with this code.

    As I said, I am very beginner, I have tried few options from Google, like for example CrossPlatformAsset and Joystick Free Asset, none of them worked for me. (first one I probably configured wrong, its very complicated for me :()