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

Android touch buttons/ Touch Controls help!!!!

Discussion in 'Android' started by Karamveer1991, Apr 30, 2014.

  1. Karamveer1991

    Karamveer1991

    Joined:
    Oct 7, 2013
    Posts:
    47
    This is the main controller script for the player in my game. Can someone please help me out in order to create two touch buttons which behave like left and right arrows does for the keyboard?
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float sideSpeed;
    7.     public float speed;
    8.     public GUIText countText;
    9.     public GUIText winText;
    10.     public GUIText fallText;
    11.     public Transform ground;
    12.     private int coins;
    13.  
    14.     void Start ()
    15.     {
    16.         coins = 0;
    17.         SetCountText ();
    18.         winText.text = "";
    19.     }
    20.    
    21.  
    22.     void Update ()
    23.     {
    24.         if (coins>=10)
    25.         {
    26.             winText.text = "You Win!!!";
    27.         }
    28.         if(transform.position.y < ground.position.y)
    29.         {
    30.             fallText.text = "You're Down!!!";
    31.         }
    32.     }
    33.  
    34.     void SetCountText()
    35.     {
    36.         countText.text = "Coins:" + coins.ToString ();
    37.     }
    38.  
    39.     void FixedUpdate()
    40.     {
    41.         float moveSideways = Input.GetAxis ("Horizontal");
    42.         Vector3 movement = new Vector3 (moveSideways, 0.0f, 0.0f);
    43.         rigidbody.AddForce (movement * sideSpeed * Time.deltaTime);
    44.         transform.position += Vector3.forward * speed * Time.deltaTime;
    45.     }
    46.  
    47.     void OnTriggerEnter(Collider other)
    48.     {
    49.         if(other.gameObject.tag=="coin")
    50.         {
    51.             other.gameObject.SetActive(false);
    52.             coins = coins + 1;
    53.             SetCountText();
    54.         }
    55.     }
    56. }
    57.  
    Thanks in advance ;)
     
  2. TrentNaylor

    TrentNaylor

    Joined:
    Apr 22, 2014
    Posts:
    27
    So a little helpful tip when it comes to basic touch controls is that Unity automatically translates many PC inputs(such as Mouse clicks) into touch inputs. So with this in mind a simple solution is to define your standard GUI.Buttons and then trigger your Left arrow or Right arrow logic.

    Code (csharp):
    1. if (GUI.Button(Rect(10,10,50,50),myTexture)){
    2. //Do my left or right button code here.
    3. }
     
  3. abhi6163

    abhi6163

    Joined:
    May 1, 2014
    Posts:
    3
    hey i am totally new to unity can you provide any kind of tutorials for this...
     
  4. TrentNaylor

    TrentNaylor

    Joined:
    Apr 22, 2014
    Posts:
    27